U
    h+                     @   s   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZmZ G dd dejZG dd dejZG d	d
 d
ejZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZdS )z
REST API Serializers for Notifications Application

This module contains Django REST Framework serializers for notification
management, channels, templates, and rules.
    )serializers)User)NotificationChannelNotificationTemplateNotificationNotificationRuleNotificationPreferenceNotificationSubscriptionNotificationHistoryc                   @   sR   e Zd ZdZejddZe Ze Z	G dd dZ
dd Zdd	 Zd
d ZdS )NotificationChannelSerializerz
    Serializer for NotificationChannel model.
    
    Handles serialization of notification channel configurations
    with validation and security considerations.
    T)	read_onlyc                   @   sF   e Zd ZeZddddddddd	d
dddgZdd	ddgZdddiiZdS )z"NotificationChannelSerializer.Metaidnamechannel_type	is_activeconfiguration
rate_limitretry_attemptstimeout_seconds
created_byis_configured	last_used
created_at
updated_at
write_onlyTN)__name__
__module____qualname__r   modelfieldsread_only_fieldsextra_kwargs r"   r"   F/var/www/html/JingleDetector/src/apps/notifications/api/serializers.pyMeta   s&             r$   c                 C   s   |  S )z(Check if channel is properly configured.)r   selfobjr"   r"   r#   get_is_configured)   s    z/NotificationChannelSerializer.get_is_configuredc                 C   s$   |j d }|r |jr |jS dS )z=Get timestamp of last notification sent through this channel.z-sent_atN)notificationsorder_byfirstsent_atr&   r'   Zlast_notificationr"   r"   r#   get_last_used-   s    z+NotificationChannelSerializer.get_last_usedc                 C   sr   | j d}|dkr4|dr(|dsntdn:|dkrR|dsntdn|d	krn|d
sntd|S )z-Validate channel configuration based on type.r   telegram	bot_tokenchat_idz3Telegram channels require 'bot_token' and 'chat_id'email
recipientsz(Email channels require 'recipients' listwebhookurlzWebhook channels require 'url')initial_datagetr   ValidationError)r&   valuer   r"   r"   r#   validate_configuration2   s"    

z4NotificationChannelSerializer.validate_configurationN)r   r   r   __doc__r   StringRelatedFieldr   SerializerMethodFieldr   r   r$   r(   r.   r:   r"   r"   r"   r#   r      s   r   c                   @   sF   e Zd ZdZe Ze ZG dd dZdd Z	dd Z
dd	 Zd
S )NotificationTemplateSerializerz
    Serializer for NotificationTemplate model.
    
    Handles template data with variable validation and usage statistics.
    c                   @   s6   e Zd ZeZddddddddd	d
ddgZdddgZdS )z#NotificationTemplateSerializer.Metar   r   template_typesubject_templatemessage_template	variableschannel_typesr   usage_countr   r   r   N)r   r   r   r   r   r   r    r"   r"   r"   r#   r$   S   s           r$   c                 C   s
   |j  S )z0Get number of times this template has been used.)r)   countr%   r"   r"   r#   get_usage_count]   s    z.NotificationTemplateSerializer.get_usage_countc                 C   s   |j d }|r|jS dS )zGet timestamp of last usage.z-created_atN)r)   r*   r+   r   r-   r"   r"   r#   r.   a   s    z,NotificationTemplateSerializer.get_last_usedc                 C   s6   t |tstdt|tt|kr2td|S )z6Validate that template variables are properly defined.zVariables must be a listzDuplicate variables not allowed)
isinstancelistr   r8   lenset)r&   r9   r"   r"   r#   validate_variablesf   s
    


z1NotificationTemplateSerializer.validate_variablesN)r   r   r   r;   r   r=   rD   r   r$   rF   r.   rK   r"   r"   r"   r#   r>   I   s   
r>   c                   @   sZ   e Zd ZdZejdddZejdddZe Z	e Z
G dd dZdd	 Zd
d ZdS )NotificationSerializerzs
    Serializer for Notification model.
    
    Provides notification data with delivery status and metadata.
    channel.nameTsourcer   template.namec                   @   sF   e Zd ZeZddddddddd	d
dddddddgZddddddgZdS )zNotificationSerializer.Metar   channeltemplatechannel_nametemplate_namesubjectmessagecontextstatusis_sentscheduled_atr,   delivery_durationretry_counterror_messager   r   N)r   r   r   r   r   r   r    r"   r"   r"   r#   r$   ~   s4                    r$   c                 C   s   |  S )z,Check if notification was successfully sent.)rY   r%   r"   r"   r#   get_is_sent   s    z"NotificationSerializer.get_is_sentc                 C   s(   |j r$|jr$|j |j }t| S dS )z!Get delivery duration in seconds.N)r,   r   inttotal_seconds)r&   r'   deltar"   r"   r#   get_delivery_duration   s    z,NotificationSerializer.get_delivery_durationN)r   r   r   r;   r   r<   rS   rT   r=   rY   r[   r$   r^   rb   r"   r"   r"   r#   rL   r   s   rL   c                   @   sB   e Zd ZdZejdddZejdddZG dd dZdd	 Z	d
S )NotificationRuleSerializerzo
    Serializer for NotificationRule model.
    
    Handles rule configuration with condition validation.
    rM   TrN   rP   c                   @   s6   e Zd ZeZddddddddd	d
ddgZdddgZdS )zNotificationRuleSerializer.Metar   r   
event_typerQ   rR   rS   rT   
conditionsr   priorityr   r   N)r   r   r   r   r   r   r    r"   r"   r"   r#   r$      s            r$   c                 C   sv   t |tstddddddddd	g}| D ]@\}}t |tr0|d
}|r0||kr0td| d| dq0|S )z Validate rule conditions format.zConditions must be a dictionaryeqnegtltgtelteincontainsoperatorzInvalid operator 'z' for field '')rG   dictr   r8   itemsr7   )r&   r9   Zallowed_operatorsfield	conditionro   r"   r"   r#   validate_conditions   s    



z.NotificationRuleSerializer.validate_conditionsN)
r   r   r   r;   r   r<   rS   rT   r$   ru   r"   r"   r"   r#   rc      s
   	rc   c                   @   sP   e Zd ZdZe Ze Ze Ze Z	e
 Ze
 Ze Ze ZdS )NotificationStatsSerializerzw
    Serializer for notification statistics data.
    
    Provides aggregated notification metrics for reporting.
    N)r   r   r   r;   r   IntegerFieldtotal_notificationssent_notificationsfailed_notificationspending_notifications
FloatFielddelivery_rateavg_delivery_time	DictFieldchannel_performance	ListFieldrecent_activityr"   r"   r"   r#   rv      s   rv   c                   @   sB   e Zd ZdZejdddZejdddZG dd dZdd	 Z	d
S ) NotificationPreferenceSerializerzs
    Serializer for NotificationPreference model.
    
    Handles user notification preferences and settings.
    user.usernameTrN   rM   c                   @   s6   e Zd ZeZddddddddd	d
ddgZdddgZdS )z%NotificationPreferenceSerializer.Metar   useruser_usernamerQ   rS   rd   
is_enabledquiet_hours_startquiet_hours_endsettingsr   r   N)r   r   r   r   r   r   r    r"   r"   r"   r#   r$      s            r$   c                 C   s2   | d}| d}|r.|r.||kr.td|S )z#Validate quiet hours configuration.r   r   z.Quiet hours start time must be before end time)r7   r   r8   )r&   dataZquiet_startZ	quiet_endr"   r"   r#   validate   s    

z)NotificationPreferenceSerializer.validateN)
r   r   r   r;   r   r<   r   rS   r$   r   r"   r"   r"   r#   r      s
   	r   c                   @   s4   e Zd ZdZejdddZG dd dZdd Zd	S )
"NotificationSubscriptionSerializerzt
    Serializer for NotificationSubscription model.
    
    Handles user subscriptions to notification topics.
    r   TrN   c                
   @   s4   e Zd ZeZddddddddd	d
g
Zddd	d
gZdS )z'NotificationSubscriptionSerializer.Metar   r   r   topicr   Z	frequencyZlast_notification_sentmetadatar   r   N)r   r   r   r	   r   r   r    r"   r"   r"   r#   r$      s          r$   c                 C   s.   ddddg}||kr*t dd| |S )zValidate frequency choice.	immediateZhourlyZdailyZweeklyzFrequency must be one of: , r   r8   join)r&   r9   Zvalid_frequenciesr"   r"   r#   validate_frequency  s    z5NotificationSubscriptionSerializer.validate_frequencyN)	r   r   r   r;   r   r<   r   r$   r   r"   r"   r"   r#   r      s   	r   c                   @   sJ   e Zd ZdZejdddZejdddZG dd dZdd	 Z	d
d Z
dS )NotificationHistorySerializerz
    Serializer for NotificationHistory model.
    
    Provides detailed tracking and analytics for notification delivery.
    znotification.subjectTrN   znotification.recipientc                	   @   s.   e Zd ZeZddddddddd	g	ZddgZd
S )z"NotificationHistorySerializer.Metar   notificationnotification_subjectnotification_recipientactionrX   	timestampZerror_detailsr   N)r   r   r   r
   r   r   r    r"   r"   r"   r#   r$     s          r$   c                 C   s2   ddddddg}||kr.t dd| |S )	zValidate action choice.createdZqueuedsentfailedretried	cancelledzAction must be one of: r   r   )r&   r9   Zvalid_actionsr"   r"   r#   validate_action  s    z-NotificationHistorySerializer.validate_actionc                 C   s.   ddddg}||kr*t dd| |S )zValidate status choice.successerrorwarninginfozStatus must be one of: r   r   )r&   r9   Zvalid_statusesr"   r"   r#   validate_status&  s    z-NotificationHistorySerializer.validate_statusN)r   r   r   r;   r   r<   r   r   r$   r   r   r"   r"   r"   r#   r     s   	r   N)r;   rest_frameworkr   django.contrib.auth.modelsr   Zapps.notifications.modelsr   r   r   r   r   r	   r
   ModelSerializerr   r>   rL   rc   
Serializerrv   r   r   r   r"   r"   r"   r#   <module>   s   $9)%&!