o
    Exiu                     @   s:   d dl mZ d dlmZ d dlmZ G dd dejZdS )    )models)User)RegexValidatorc                   @   s  e Zd ZdZejdddZejdedddgd	d
Zej	ddddZ
ejddddZejeddZejeddZejdddZejddddZejdddZejeejdddZejddZejddZejdddZej	d d!dZejd"d#dZd$d% Z d&d' Z!G d(d) d)Z"d*d+ Z#d,S )-Seniorz9Model representing a senior person who will receive callsd   zFull name of the senior)
max_length	help_text   z^\+?1?\d{9,15}$zRPhone number must be entered in the format: '+999999999'. Up to 15 digits allowed.)regexmessagez8Phone number in international format (e.g., +1234567890))r   
validatorsr   TzAge of the senior)nullblankr   2   UTCzTimezone for scheduling calls)r   defaultr   z=List of preferred call times (e.g., ['morning', 'afternoon']))r   r   z9List of interests and topics the senior enjoys discussingz<Any health-related notes or considerations for conversations)r   r   zqName of the AI companion for this senior (e.g., 'Tom'). Can be used in first_message template as {companion.name})r   r   r   zCustom first message template for calls. Use {senior.name} for senior's name and {companion.name} for companion name. If empty, uses default greeting.seniorsz=The caregiver or family member managing this senior's profile)	on_deleterelated_namer   )auto_now_add)auto_nowz'Whether this senior is currently active<   z1Monthly call allowance in minutes for this seniorr   z3Extra minutes balance added by admin (carried over)c                 C   sn   ddl m} ddlm} ddlm} | }|jdddddd}|jj	| d|dj
|d	d
d p2d}|d S )z>Calculate total call duration in minutes for the current monthr   )timezone)Call)Sum   )dayhourminutesecondmicrosecond	completed)seniortwilio_statuscall_start_time__gteduration)totalr&   r   )django.utilsr   calls.modelsr   django.db.modelsr   nowreplaceobjectsfilter	aggregate)selfr   r   r   r*   first_day_of_monthtotal_seconds r2   '/var/www/html/philips/seniors/models.pyget_monthly_usage_minutes   s"   z Senior.get_monthly_usage_minutesc                 C   s"   |   }| j| j | }td|S )zSCalculate total available minutes (monthly allowance + extra balance - month usage)r   )r4   monthly_timelimitextra_minutes_balancemax)r/   usage	availabler2   r2   r3   get_available_minutes   s   
zSenior.get_available_minutesc                   @   s   e Zd ZdgZdS )zSenior.MetanameN)__name__
__module____qualname__orderingr2   r2   r2   r3   Meta   s    
r@   c                 C   s   | j  d| j dS )Nz ())r;   phone_number)r/   r2   r2   r3   __str__   s   zSenior.__str__N)$r<   r=   r>   __doc__r   	CharFieldr;   r   rB   PositiveIntegerFieldager   	JSONFieldlistpreferred_call_times	interests	TextFieldhealth_notescompanion_namefirst_message
ForeignKeyr   CASCADE	caregiverDateTimeField
created_at
updated_atBooleanField	is_activer5   
FloatFieldr6   r4   r:   r@   rC   r2   r2   r2   r3   r   I   sn    
r   N)	django.dbr   django.contrib.auth.modelsr   django.core.validatorsr   Modelr   r2   r2   r2   r3   <module>   s    F