U
    	h;1                     @   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	d
g	Zdddd
d	gZdd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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channelcategory	is_activesimilarity_thresholdget_detection_countimage_status
created_by
created_atslugdescriptionchannel__namechannel__slugzTemplate Informationfields)r   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-   6/var/www/html/JingleDetector/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dS )z(Optimize queryset with prefetch_related.r   r   superget_querysetselect_relatedr*   request	__class__r-   r.   r2   a   s    z JingleTemplateAdmin.get_queryset)__name__
__module____qualname____doc__list_displaylist_filtersearch_fields	fieldsetsprepopulated_fieldsreadonly_fieldsorderingr   short_descriptionr   r2   __classcell__r-   r-   r6   r.   r
      sX   	                 
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*   r5   r+   r-   r-   r.   has_add_permissionr   s    z(JingleDetectionInline.has_add_permission)N)
r8   r9   r:   r;   r   modelextrarA   r   rI   r-   r-   r-   r.   rE   f   s      rE   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_inforF   sessionrG   r    rH   session__channeltemplate__namesession__channel__namezDetection Informationr   )rN   segmentrF   zDetection Results)rG   frame_timestampr    z
Frame Data)
frame_pathrH   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>)rG   r    r   )r*   r+   Zconfidence_colorZconfirmation_textZconfirmation_colorr-   r-   r.   rM      s     z'JingleDetectionAdmin.get_detection_infoZ	Detectionc                    s   t  |dddS )&Optimize queryset with select_related.rF   rO   rR   r0   r4   r6   r-   r.   r2      s
      z!JingleDetectionAdmin.get_queryset)r8   r9   r:   r;   r<   r=   r>   r?   rA   rB   rM   rC   admin_order_fieldr2   rD   r-   r-   r6   r.   rL   w   sT   	             rL   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_apirO   rQ   zAd Break Informationr   )rN   r[   r\   r_   Timing)r]   end_timeduration_secondszDetection References)start_detectionend_detectionzExternal Integration)r`   api_responseZNotes)notesr   r   r   r   rc   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&   rV   )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.   rZ      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)-)rc   is_completecalculate_duration)r*   r+   minutessecondsdurationr-   r-   r.   r^     s    

zAdBreakAdmin.duration_displayDurationc                    s   t  |dddS )rW   rO   Zstart_detection__templateZend_detection__templater0   r4   r6   r-   r.   r2   (  s
      zAdBreakAdmin.get_queryset)r8   r9   r:   r;   r<   r=   r>   r?   rA   inlinesrB   rZ   rC   rX   r^   r2   rD   r-   r-   r6   r.   rY      sh   	                rY   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_inforF   rN   total_detectionsaccuracy_rate_displayavg_confidencelast_detectionrO   rP   rQ   zStatistics Overviewr   )rN   rF   zDetection Counts)r|   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.   r{   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.   r}   p  s    z.DetectionStatisticsAdmin.accuracy_rate_displayZAccuracyc                    s   t  |ddS )rW   rF   rO   r0   r4   r6   r-   r.   r2   ~  s     z%DetectionStatisticsAdmin.get_querysetc                 C   s   dS )z&Disable manual creation of statistics.Fr-   r4   r-   r-   r.   rI     s    z+DetectionStatisticsAdmin.has_add_permission)r8   r9   r:   r;   r<   r=   r>   r?   rA   rB   r{   rC   r}   rX   r2   rI   rD   r-   r-   r6   r.   rz   /  sh   	                   rz   N)r;   Zdjango.contribr   django.utils.htmlr   django.urlsr   django.utils.safestringr   Zapps.jingles.modelsr   r   r   r	   register
ModelAdminr
   TabularInlinerE   rL   rY   rz   r-   r-   r-   r.   <module>   s   TIm