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	 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NotificationRulec                   @   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   G/var/www/html/StreamProcessor/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_configuration1   s"    

z4NotificationChannelSerializer.validate_configurationN)r   r   r   __doc__r   StringRelatedFieldr   SerializerMethodFieldr   r   r!   r%   r+   r7   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!   R   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+   `   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   r5   lenset)r#   r6   r   r   r    validate_variablese   s
    


z1NotificationTemplateSerializer.validate_variablesN)r   r   r   r8   r   r:   rA   r   r!   rC   r+   rH   r   r   r   r    r;   H   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.)rV   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   r8   r   r9   rP   rQ   r:   rV   rX   r!   r[   r_   r   r   r   r    rI   q   s   rI   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.
    rJ   TrK   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NotificationRuleSerializer.Metar
   r   
event_typerN   rO   rP   rQ   
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 '')rD   dictr   r5   itemsr4   )r#   r6   Zallowed_operatorsfield	conditionrl   r   r   r    validate_conditions   s    



z.NotificationRuleSerializer.validate_conditionsN)
r   r   r   r8   r   r9   rP   rQ   r!   rr   r   r   r   r    r`      s
   	r`   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   r8   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    rs      s   rs   N)r8   rest_frameworkr   django.contrib.auth.modelsr   Zapps.notifications.modelsr   r   r   r   ModelSerializerr   r;   rI   r`   
Serializerrs   r   r   r   r    <module>   s   9)%&