U
    h0                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZ ee
G dd dejZG d	d
 d
ejZeeG dd dejZeeG dd dejZeeG dd dejZdS )z
Django Admin Configuration for Jingles Application

This module configures the Django admin interface for jingle detection,
ad break management, and related functionality.
    )admin)format_html)reverse)	mark_safe)JingleTemplateJingleDetectionAdBreakDetectionStatisticsc                       s   e Zd ZdZdddddddd	gZddd	dgZdd
dgZdddifdddifddddffZd
diZd	dgZ	ddgZ
dd Zde_dd Zde_ fddZ  ZS )JingleTemplateAdminz
    Admin interface for JingleTemplate model.
    
    Provides management of jingle reference templates including
    image upload, similarity threshold configuration, and status monitoring.
    namecategory	is_activesimilarity_thresholdget_detection_countimage_status
created_by
created_atslugdescriptionzTemplate Informationfields)r   r   r   r   zDetection Configuration)
image_pathr   r   Metadata)r   collapser   classes)r   
updated_atc                 C   sb   |j  }|j jdd }|dkrZ|| d }|dkr<dn|dkrHdnd	}td
|||S tdS )z5Get the total number of detections for this template.T)is_confirmedr   d   P   green2   orangeredz;<span style="color: {};">{} total<br/>({} confirmed)</span>z/<span style="color: gray;">No detections</span>)
detectionscountfilterr   )selfobjr%   Zconfirmed_countZ
percentagecolor r*   7/var/www/html/StreamProcessor/src/apps/jingles/admin.pyr   A   s    
z'JingleTemplateAdmin.get_detection_countZ
Detectionsc                 C   s   |  rtdS tdS dS )z(Check if the template image file exists.u0   <span style="color: green;">✓ Available</span>u,   <span style="color: red;">✗ Missing</span>N)image_existsr   )r'   r(   r*   r*   r+   r   T   s    z JingleTemplateAdmin.image_statusz
Image Filec                    s   t  |dS )z(Optimize queryset with prefetch_related.r   superget_querysetselect_relatedr'   request	__class__r*   r+   r/   a   s    z JingleTemplateAdmin.get_queryset)__name__
__module____qualname____doc__list_displaylist_filtersearch_fields	fieldsetsprepopulated_fieldsreadonly_fieldsorderingr   short_descriptionr   r/   __classcell__r*   r*   r3   r+   r
      sP   	             r
   c                   @   s:   e Zd ZdZeZdZddddgZddddgZd
dd	Z	dS )JingleDetectionInlinez>
    Inline admin for jingle detections within ad breaks.
    r   templateconfidence_scoredetection_timer   Nc                 C   s   dS )z(Disable adding detections through admin.Fr*   )r'   r2   r(   r*   r*   r+   has_add_permissionr   s    z(JingleDetectionInline.has_add_permission)N)
r5   r6   r7   r8   r   modelextrar>   r   rF   r*   r*   r*   r+   rB   f   s      rB   c                       s   e Zd ZdZddddddgZddddgZd	d
gZdddifdddifdddifddddffZdddgZdgZ	dd Z
de
_de
_ fddZ  ZS )JingleDetectionAdminz
    Admin interface for JingleDetection model.
    
    Provides detailed management of individual jingle detections
    including confidence scores, frame analysis, and confirmation status.
    get_detection_inforC   sessionrD   r   rE   session__channeltemplate__namesession__channel__namezDetection Informationr   )rK   segmentrC   zDetection Results)rD   frame_timestampr   z
Frame Data)
frame_pathrE   zAdditional Metadata)metadatar   r   r   r   z-detection_timec                 C   sL   |j dkrdn|j dkrdnd}|jr*dnd}|jr8dnd}td	|j ||S )
z$Get formatted detection information.g?r    g      ?r"   r#   u   ✓ Confirmedz? Unconfirmedgrayz><strong>{:.3f}</strong><br/><span style="color: {};">{}</span>)rD   r   r   )r'   r(   Zconfidence_colorZconfirmation_textZconfirmation_colorr*   r*   r+   rJ      s     z'JingleDetectionAdmin.get_detection_infoZ	Detectionc                    s   t  |dddS )&Optimize queryset with select_related.rC   rL   rO   r-   r1   r3   r*   r+   r/      s
      z!JingleDetectionAdmin.get_queryset)r5   r6   r7   r8   r9   r:   r;   r<   r>   r?   rJ   r@   admin_order_fieldr/   rA   r*   r*   r3   r+   rI   w   sT   	             rI   c                       s   e Zd ZdZdddddddgZddddd	gZdd
gZdddifdddifdddifdddifddddffZdddgZg Z	dgZ
dd Zde_de_dd  Zd!e_de_ fd"d#Z  ZS )$AdBreakAdminz
    Admin interface for AdBreak model.
    
    Provides comprehensive management of advertisement breaks including
    timing analysis, external API integration, and status tracking.
    get_adbreak_infochannel_nameregion
start_timeduration_displaystatusis_sent_to_apirL   rN   zAd Break Informationr   )rK   rX   rY   r\   Timing)rZ   end_timeduration_secondszDetection References)start_detectionend_detectionzExternal Integration)r]   api_responseZNotes)notesr   r   r   r   r`   z-start_timec                 C   sJ   ddddd |jd}|jr"dnd}|jr0dnd}td	||j ||S )
z#Get formatted ad break information.r"   r    r#   rS   )active	completedfailed	cancelledblacku	   📤 Sentu   📥 PendingzK<span style="color: {};">{}</span><br/><small style="color: {};">{}</small>)getr\   r]   r   title)r'   r(   status_colorZ
api_statusZ	api_colorr*   r*   r+   rW      s"     zAdBreakAdmin.get_adbreak_infoZStatusc                 C   sv   |j r>|j d }|j d }|dkr2| d| dS | dS n4| rr| }|rr|d }|d }| d| dS dS )z/Display ad break duration in a readable format.<   r   zm szs (calc)-)r`   is_completecalculate_duration)r'   r(   minutessecondsdurationr*   r*   r+   r[     s    

zAdBreakAdmin.duration_displayDurationc                    s   t  |dddS )rT   rL   Zstart_detection__templateZend_detection__templater-   r1   r3   r*   r+   r/   (  s
      zAdBreakAdmin.get_queryset)r5   r6   r7   r8   r9   r:   r;   r<   r>   inlinesr?   rW   r@   rU   r[   r/   rA   r*   r*   r3   r+   rV      sh   	                rV   c                
       s   e Zd ZdZdddddddgZdd	dgZd
dgZdddifdddifdddifdddiffZddddddddddg
ZdgZ	dd Z
de
_d d! Zd"e_de_ fd#d$Zd%d& Z  ZS )'DetectionStatisticsAdminz
    Admin interface for DetectionStatistics model.
    
    Provides analysis and reporting of jingle detection performance
    including accuracy metrics and trend analysis.
    get_stats_inforC   rK   total_detectionsaccuracy_rate_displayavg_confidencelast_detectionrL   rM   rN   zStatistics Overviewr   )rK   rC   zDetection Counts)ry   confirmed_detectionsfalse_positiveszConfidence Metrics)r{   min_confidencemax_confidencezPerformance Metrics)detection_rater|   r   r   r}   r~   r   r   r   z-last_detectionc                 C   s0   |  }|dkrdn|dkr dnd}td||S )z%Get formatted statistics information.r   r    r!   r"   r#   zE<strong>{:.1f}%</strong><br/><span style="color: {};">Accuracy</span>accuracy_rater   )r'   r(   ZaccuracyZaccuracy_colorr*   r*   r+   rx   b  s    z'DetectionStatisticsAdmin.get_stats_infoZPerformancec                 C   s0   |  }|dkrdn|dkr dnd}td||S )z(Display accuracy rate with color coding.r   r    r!   r"   r#   z'<span style="color: {};">{:.1f}%</span>r   )r'   r(   rater)   r*   r*   r+   rz   p  s    z.DetectionStatisticsAdmin.accuracy_rate_displayZAccuracyc                    s   t  |ddS )rT   rC   rL   r-   r1   r3   r*   r+   r/   ~  s     z%DetectionStatisticsAdmin.get_querysetc                 C   s   dS )z&Disable manual creation of statistics.Fr*   r1   r*   r*   r+   rF     s    z+DetectionStatisticsAdmin.has_add_permission)r5   r6   r7   r8   r9   r:   r;   r<   r>   r?   rx   r@   rz   rU   r/   rF   rA   r*   r*   r3   r+   rw   /  sh   	                   rw   N)r8   Zdjango.contribr   django.utils.htmlr   django.urlsr   django.utils.safestringr   Zapps.jingles.modelsr   r   r   r	   register
ModelAdminr
   TabularInlinerB   rI   rV   rw   r*   r*   r*   r+   <module>   s   TIm