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G dd dejZdS )z
REST API Serializers for Jingles Application

This module contains Django REST Framework serializers for jingle detection,
ad break management, and related functionality.
    )serializers)User)JingleTemplateJingleDetectionAdBreakDetectionStatisticsc                   @   sZ   e Zd ZdZejddZe Ze Z	e Z
G dd dZdd Zdd	 Zd
d ZdS )JingleTemplateSerializerz
    Serializer for JingleTemplate model.
    
    Handles serialization of jingle template data including
    image information and detection statistics.
    T)	read_onlyc                   @   s<   e Zd ZeZddddddddd	d
ddddgZdd	ddgZdS )zJingleTemplateSerializer.Metaidnameslugdescription
image_pathsimilarity_threshold	is_activecategory
created_byimage_existsdetection_countaccuracy_rate
created_at
updated_atN)__name__
__module____qualname__r   modelfieldsread_only_fields r   r   A/var/www/html/StreamProcessor/src/apps/jingles/api/serializers.pyMeta   s"             r    c                 C   s   |  S )z(Check if the template image file exists.)r   selfobjr   r   r   get_image_exists$   s    z)JingleTemplateSerializer.get_image_existsc                 C   s
   |j  S )z1Get total number of detections for this template.)
detectionscountr!   r   r   r   get_detection_count(   s    z,JingleTemplateSerializer.get_detection_countc                 C   s:   |j  }|dkrdS |j jdd }t|| d dS )z$Get accuracy rate for this template.r   T)is_confirmedd      )r%   r&   filterround)r"   r#   totalZ	confirmedr   r   r   get_accuracy_rate,   s
    
z*JingleTemplateSerializer.get_accuracy_rateN)r   r   r   __doc__r   StringRelatedFieldr   SerializerMethodFieldr   r   r   r    r$   r'   r.   r   r   r   r   r      s   
r   c                   @   sX   e Zd ZdZejdddZejdddZejdddZe	 Z
G dd dZd	d
 ZdS )JingleDetectionSerializerz
    Serializer for JingleDetection model.
    
    Provides detailed detection information including session
    and template data for analysis and monitoring.
    template.nameTsourcer	   session.channel.namezsegment.filenamec                   @   s@   e Zd ZeZddddddddd	d
ddddddgZddddgZdS )zJingleDetectionSerializer.Metar
   sessionsegmenttemplatetemplate_namechannel_namesegment_filenameconfidence_score
frame_pathframe_existsdetection_timeframe_timestampr(   metadatar   r   N)r   r   r   r   r   r   r   r   r   r   r   r    B   s.                 r    c                 C   s   |  S )z)Check if the extracted frame file exists.)r?   r!   r   r   r   get_frame_existsO   s    z*JingleDetectionSerializer.get_frame_existsN)r   r   r   r/   r   r0   r:   r;   r<   r1   r?   r    rC   r   r   r   r   r2   5   s   r2   c                   @   sh   e Zd ZdZejdd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 )AdBreakSerializerz
    Serializer for AdBreak model.
    
    Handles ad break data including timing information,
    detection references, and external API status.
    r6   Tr4   zstart_detection.template.namezend_detection.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ddgZddddgZdS )zAdBreakSerializer.Metar
   r7   r;   regionstatusstart_detectionend_detectionstart_template_nameend_template_name
start_timeend_timeduration_secondsduration_formattedis_completeis_sent_to_apiapi_responsenotesr   r   N)r   r   r   r   r   r   r   r   r   r   r   r    b   s4                	   r    c                 C   s@   |j r<|j d }|j d }|dkr2| d| dS | dS dS )z#Get human-readable duration format.<   r   zm sN)rM   )r"   r#   minutessecondsr   r   r   get_duration_formattedp   s    


z(AdBreakSerializer.get_duration_formattedc                 C   s   |  S )zCheck if ad break is complete.)rO   r!   r   r   r   get_is_complete{   s    z!AdBreakSerializer.get_is_completeN)r   r   r   r/   r   r0   r;   rI   rJ   r1   rN   rO   r    rW   rX   r   r   r   r   rD   T   s   rD   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 )DetectionStatisticsSerializerz
    Serializer for DetectionStatistics model.
    
    Provides performance metrics and analysis data
    for jingle detection accuracy and effectiveness.
    r3   Tr4   r6   c                   @   sP   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dddddgZdS )z"DetectionStatisticsSerializer.Metar
   r7   r9   r:   r;   total_detectionsconfirmed_detectionsfalse_positivesr   false_positive_rateavg_confidencemin_confidencemax_confidencedetection_ratelast_detectionr   r   N)r   r   r   r   r   r   r   r   r   r   r   r       s>                      r    c                 C   s   |  S )zGet accuracy rate percentage.)r   r!   r   r   r   r.      s    z/DetectionStatisticsSerializer.get_accuracy_ratec                 C   s   |  S )z#Get false positive rate percentage.)r]   r!   r   r   r   get_false_positive_rate   s    z5DetectionStatisticsSerializer.get_false_positive_rateN)r   r   r   r/   r   r0   r:   r;   r1   r   r]   r    r.   rc   r   r   r   r   rY      s   rY   c                   @   sH   e Zd ZdZe ZejddZejddZ	e
 Ze
 Ze ZdS )AdBreakDataSerializerz
    Serializer for ad break data export to external APIs.
    
    Formats ad break information for external system integration
    according to API specifications.
    T)
allow_nullN)r   r   r   r/   r   DateTimeFieldstartendIntegerFieldduration	CharFieldchannelrE   	UUIDFieldZad_break_idr   r   r   r   rd      s   rd   c                   @   s@   e Zd ZdZe Ze Ze Ze	 Z
e Ze ZdS )DetectionSummarySerializerz
    Serializer for detection summary statistics.
    
    Provides aggregated detection data for reporting
    and dashboard display purposes.
    N)r   r   r   r/   r   ri   rZ   detections_todayr[   
FloatFieldr   	ListFieldtop_templatesrecent_activityr   r   r   r   rn      s   rn   N)r/   rest_frameworkr   django.contrib.auth.modelsr   apps.jingles.modelsr   r   r   r   ModelSerializerr   r2   rD   rY   
Serializerrd   rn   r   r   r   r   <module>   s   (,&