o
    *dhT                     @   s`  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
 d dlmZmZ ddlmZmZmZmZmZmZ eeG d	d
 d
ejZeeG dd dejZ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eeG dd dejZedej _!edej _"edej _#dS )    )admin)format_html)gettext_lazy)reverse)	mark_safe)CountSum   )GeographicZoneBroadcastNetwork	TVChannelChannelCoverageContentScheduleAudienceDemographicsc                       s   e Zd ZdZg dZg dZg dZddgZeddd	ifed
dddfeddddfedddiffZ	ddgZ
dd Zede_dd Zede_dd Zede_dd Zede_ fd d!Z  ZS )"GeographicZoneAdminz
    Admin interface for Geographic Zones.
    
    Provides comprehensive management of geographic zones with hierarchical
    display, population statistics, and coverage analytics.
    )	namecode	zone_typeparent_zone_linkpopulation_formattedtv_households_formattedchild_zones_count	is_active
created_at)r   r   r   Zparent_zone__zone_type)r   r   Zparent_zone__namer   r   Basic Informationfields)r   r   r   parent_zoneZDemographics)
populationtv_householdscollapser   classeszGeographic Data)latitude	longitudetimezoneStatusr   r   
updated_atc                 C   ,   |j rtd|j jgd}td||j jS dS )z(Display parent zone as a clickable link.$admin:channels_geographiczone_changeargs<a href="{}">{}</a>-N)r   r   pkr   r   selfobjurl r4   GC:\Users\vibe-look\OneDrive\Desktop\Adtlas_V\src\apps\channels\admin.pyr   K      z$GeographicZoneAdmin.parent_zone_linkzParent Zonec                 C      |j r|j dS dS )z,Format population with thousands separators.,r.   N)r   r1   r2   r4   r4   r5   r   S      
z(GeographicZoneAdmin.population_formatted
Populationc                 C   r7   )z/Format TV households with thousands separators.r8   r.   N)r   r9   r4   r4   r5   r   Z   r:   z+GeographicZoneAdmin.tv_households_formattedzTV Householdsc                 C   s   |  }|dkrtd|S dS )zDisplay count of child zones.r   -<span style="color: #0066cc;">{} zones</span>0N)get_child_zones_countr   r1   r2   countr4   r4   r5   r   a   s   z%GeographicZoneAdmin.child_zones_countzChild Zonesc                       t  |dS )&Optimize queryset with select_related.r   Nsuperget_querysetselect_relatedr1   request	__class__r4   r5   rE   l      z GeographicZoneAdmin.get_queryset)__name__
__module____qualname____doc__list_displaylist_filtersearch_fieldsordering_	fieldsetsreadonly_fieldsr   short_descriptionr   r   r   rE   __classcell__r4   r4   rI   r5   r      s>    



	r   c                       s   e Zd ZdZg dZg dZg dZddgZeddd	ifed
dddfeddddfeddddfedddiffZ	ddgZ
dd Zede_dd Zede_dd Zede_ fdd Z  ZS )!BroadcastNetworkAdminz
    Admin interface for Broadcast Networks.
    
    Manages TV networks with hierarchical relationships, channel counts,
    and network-specific metadata.
    )r   
short_namenetwork_typeparent_network_linkchannels_countsubsidiaries_countfounded_dater   )r[   r   r_   r   )r   rZ   headquarters_locationr[   r   r   r   )r   rZ   r[   parent_networkzCompany Details)r_   r`   descriptionr   r!   Online Presencewebsite_urllogo_urlzAudience Data)target_demographicsr&   r'   r   r(   c                 C   r)   )z+Display parent network as a clickable link.&admin:channels_broadcastnetwork_changer+   r-   r.   N)ra   r   r/   r   r   r0   r4   r4   r5   r\      r6   z)BroadcastNetworkAdmin.parent_network_linkzParent Networkc                 C   s   |j }|dkrtd|S dS )z+Display count of channels for this network.r   z0<span style="color: #0066cc;">{} channels</span>r=   N)channel_countr   r?   r4   r4   r5   r]      s   z$BroadcastNetworkAdmin.channels_countZChannelsc                 C   (   |j jdd }|dkrtd|S dS )z%Display count of subsidiary networks.Tr'   r   z4<span style="color: #009900;">{} subsidiaries</span>r=   N)subsidiary_networksfilterr@   r   r?   r4   r4   r5   r^         z(BroadcastNetworkAdmin.subsidiaries_countZSubsidiariesc                    s*   t  |djtdtjddddS )z6Optimize queryset with select_related and annotations.ra   channelsT)Zchannels__is_active)rl   )r]   N)rD   rE   rF   annotater   modelsQrG   rI   r4   r5   rE      s   z"BroadcastNetworkAdmin.get_queryset)rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   r\   rW   r]   r^   rE   rX   r4   r4   rI   r5   rY   q   sB    

	
	rY   c                       0   e Zd ZdZeZdZg dZ fddZ  Z	S )ChannelCoverageInlinez
    Inline admin for Channel Coverage within TV Channel admin.
    
    Allows managing channel coverage zones directly from the channel edit page.
    r	   )zonesignal_strengthcoverage_percentagesubscriber_countpenetration_rater   c                    rA   )zOptimize inline queryset.rt   NrC   rG   rI   r4   r5   rE      rK   z"ChannelCoverageInline.get_queryset)
rL   rM   rN   rO   r   modelextrar   rE   rX   r4   r4   rI   r5   rs      s    	rs   c                       rr   )ContentScheduleInlinez
    Inline admin for Content Schedule within TV Channel admin.
    
    Shows recent and upcoming scheduled content for the channel.
    r   )titlecontent_type
start_timeduration_minutesis_livec                    sL   ddl m} ddlm} | |dd }t |j|dddd	 S )
z/Show only recent and upcoming schedule entries.r   )r%   )	timedelta   )days)Zstart_time__gter~   N
   )	django.utilsr%   datetimer   nowrD   rE   rl   order_by)r1   rH   r%   r   Zcutoff_daterI   r4   r5   rE      s   z"ContentScheduleInline.get_queryset)
rL   rM   rN   rO   r   ry   rz   r   rE   rX   r4   r4   rI   r5   r{      s    r{   c                       s   e Zd ZdZg dZg dZg dZddgZeddd	ifed
dddfeddddfeddddfeddddfedddiffZ	ddgZ
eegZdd Zede_dd Zede_dd Zed e_d!d" Zed#e_ fd$d%Z  ZS )&TVChannelAdminz
    Admin interface for TV Channels.
    
    Comprehensive channel management with coverage zones, scheduling,
    and audience analytics integration.
    )r   	call_signchannel_numberchannel_typenetwork_linkcontent_categorycoverage_zones_counttotal_populationhd_uhd_statusadvertising_enabledr   )	r   r   Znetwork__network_typer   
is_premiumr   hd_availableuhd_4k_availabler   )r   r   r   Znetwork__nameZnetwork__short_namer   r   r   r   )r   r   r   r   networkzContent & Programming)r   primary_languagesecondary_languagesrg   rb   r   r!   zTechnical Specifications)r   r   rc   rd   zBusiness Settings)r   r   launch_dater&   r'   r   r(   c                 C   s*   t d|jjgd}td||jjp|jjS )z$Display network as a clickable link.rh   r+   r-   N)r   r   r/   r   rZ   r   r0   r4   r4   r5   r   Y  s   zTVChannelAdmin.network_linkNetworkc                 C   rj   )z Display count of coverage zones.Tr'   r   r<   r=   N)coverage_zonesrl   r@   r   r?   r4   r4   r5   r   c  rm   z#TVChannelAdmin.coverage_zones_countzCoverage Zonesc                 C   s   |j }|dkr|dS dS )z!Display total population covered.r   r8   r.   N)total_coverage_population)r1   r2   r   r4   r4   r5   r   n  s   zTVChannelAdmin.total_populationzTotal Populationc                 C   s>   g }|j r
|d |jr|d |rtd|S tdS )z#Display HD/UHD availability status.z'<span style="color: #009900;">HD</span>z'<span style="color: #0066cc;">4K</span>z | z$<span style="color: #999;">SD</span>N)r   appendr   r   join)r1   r2   statusr4   r4   r5   r   v  s   

zTVChannelAdmin.hd_uhd_statusZQualityc                    rA   )rB   r   NrC   rG   rI   r4   r5   rE     rK   zTVChannelAdmin.get_queryset)rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rs   r{   inlinesr   rW   r   r   r   rE   rX   r4   r4   rI   r5   r     sP    
#

	

r   c                       s   e Zd ZdZg dZg dZg dZddgZeddd	ifed
ddifeddddfeddddfedddiffZ	ddgZ
dd Zede_dd Zede_dd Zede_dd  Zed!e_ fd"d#Z  ZS )$ChannelCoverageAdminz
    Admin interface for Channel Coverage relationships.
    
    Manages the detailed coverage information between channels and zones.
    )channel_name	zone_nameru   rv   subscriber_count_formattedrx   estimated_viewers_formattedr   )ru   r   Zchannel__channel_typezone__zone_typelaunch_date_in_zone)channel__namechannel__call_sign
zone__nameZ
zone__coder   r   ZRelationshipr   )channelrt   zCoverage Details)ru   rv   rw   rx   ZTimeline)r   r   r!   zAdditional Information)notesr&   r'   r   r(   c                 C   "   t d|jjgd}td||jjS )zDisplay channel name with link.admin:channels_tvchannel_changer+   r-   Nr   r   r/   r   r   r0   r4   r4   r5   r        z!ChannelCoverageAdmin.channel_nameChannelc                 C   r   )zDisplay zone name with link.r*   r+   r-   Nr   rt   r/   r   r   r0   r4   r4   r5   r     r   zChannelCoverageAdmin.zone_nameZonec                 C   r7   )z2Format subscriber count with thousands separators.r8   r.   N)rw   r9   r4   r4   r5   r     r:   z/ChannelCoverageAdmin.subscriber_count_formattedZSubscribersc                 C   s   |j }|r	|dS dS )z3Format estimated viewers with thousands separators.r8   r.   N)estimated_viewers)r1   r2   Zviewersr4   r4   r5   r     s   z0ChannelCoverageAdmin.estimated_viewers_formattedzEst. Viewersc                       t  |ddS rB   r   rt   NrC   rG   rI   r4   r5   rE        z!ChannelCoverageAdmin.get_queryset)rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   r   rW   r   r   r   rE   rX   r4   r4   rI   r5   r     sD    



r   c                	       s   e Zd ZdZg dZg dZg dZdgZdZe	ddd	ife	d
ddife	ddddfe	ddddfe	dddife	ddddffZ
ddgZdd Ze	de_dd Ze	de_dd Ze	de_d d! Ze	d"e_ fd#d$Z  ZS )%ContentScheduleAdminz
    Admin interface for Content Schedule (EPG).
    
    Manages programming schedules with content metadata and advertising breaks.
    )	r|   channel_linkr}   r~   duration_formattedadvertising_infor   is_premierecurrently_airing)r}   r   r   channel__networkr~   genrerating)r|   rb   r   r   r   z-start_timer~   zProgram Informationr   )r   r|   rb   r}   Schedule)r~   end_timer   zContent Details)r   r   rg   r   r!   ZAdvertising)advertising_breaksad_break_durationZFlags)r   r   zExternal Integration)external_idr   r(   c                 C   r   z$Display channel as a clickable link.r   r+   r-   Nr   r0   r4   r4   r5   r   +  r   z!ContentScheduleAdmin.channel_linkr   c                 C   s2   t |jd\}}|dkr| d| dS | dS )z%Format duration in hours and minutes.<   r   zh mN)divmodr   )r1   r2   hoursminutesr4   r4   r5   r   1  s   
z'ContentScheduleAdmin.duration_formattedDurationc                 C   s*   |j dkr|jd }td|j |S tdS )z&Display advertising break information.r   r   z4<span style="color: #009900;">{} breaks ({}m)</span>z(<span style="color: #999;">No ads</span>N)r   r   r   )r1   r2   Ztotal_minutesr4   r4   r5   r   9  s   

z%ContentScheduleAdmin.advertising_infoc                 C   s   |j rtdS dS )z(Show if the program is currently airing.u-   <span style="color: #ff0000;">● LIVE</span>r.   N)is_currently_airingr   r9   r4   r4   r5   r   E  s   z%ContentScheduleAdmin.currently_airingr&   c                    rA   )rB   r   NrC   rG   rI   r4   r5   rE   L  rK   z!ContentScheduleAdmin.get_queryset)rL   rM   rN   rO   rP   rQ   rR   rS   date_hierarchyrT   rU   rV   r   rW   r   r   r   rE   rX   r4   r4   rI   r5   r     sN    





r   c                       s   e Zd ZdZg dZg dZg dZddgZdZe	dd	d
ife	ddddfe	ddddffZ
ddgZdd Ze	de_dd Ze	de_dd Ze	de_dd Ze	de_dd  Ze	d!e_d"d# Ze	d$e_ fd%d&Z  ZS )'AudienceDemographicsAdminz
    Admin interface for Audience Demographics.
    
    Manages viewership analytics and demographic data for channels and zones.
    )r   	zone_linkmeasurement_datetotal_viewers_formattedprimary_age_group_displayengagement_scoreviewing_hours_formatted)r   r   r   Zchannel__content_category)r   r   r   z-measurement_dater   r   zMeasurement Detailsr   )r   rt   r   total_viewerszDemographics Distribution)age_distributiongender_distributionincome_distributioneducation_distributionr   r!   zViewing Behavior)viewing_hours_per_weekpeak_viewing_timesr   r(   c                 C   r   r   r   r0   r4   r4   r5   r     r   z&AudienceDemographicsAdmin.channel_linkr   c                 C   r   )z!Display zone as a clickable link.r*   r+   r-   Nr   r0   r4   r4   r5   r     r   z#AudienceDemographicsAdmin.zone_linkr   c                 C   s
   |j dS )z/Format total viewers with thousands separators.r8   N)r   r9   r4   r4   r5   r     s   
z1AudienceDemographicsAdmin.total_viewers_formattedzTotal Viewersc                 C   s   |j }|r
td|S dS )zDisplay the primary age group.z'<span style="color: #0066cc;">{}</span>r.   N)primary_age_groupr   )r1   r2   Z	age_groupr4   r4   r5   r     s   z3AudienceDemographicsAdmin.primary_age_group_displayzPrimary Age Groupc                 C   s2   |j }|dkr
d}n	|dkrd}nd}td||S )z Display viewer engagement score.K   z#0099002   z#ff9900z#cc0000z'<span style="color: {};">{:.1f}%</span>N)viewer_engagement_scorer   )r1   r2   scorecolorr4   r4   r5   r     s   z*AudienceDemographicsAdmin.engagement_scoreZ
Engagementc                 C   s   |j r
|j ddS dS )zFormat viewing hours per week.z.1fzh/weekr.   N)r   r9   r4   r4   r5   r     s   z1AudienceDemographicsAdmin.viewing_hours_formattedzViewing Hoursc                    r   r   rC   rG   rI   r4   r5   rE     r   z&AudienceDemographicsAdmin.get_queryset)rL   rM   rN   rO   rP   rQ   rR   rS   r   rT   rU   rV   r   rW   r   r   r   r   r   rE   rX   r4   r4   rI   r5   r   Q  sB    
	



	

r   zAdtlas TV Advertising PlatformzAdtlas Adminz!TV Channels & Networks ManagementN)$Zdjango.contribr   django.utils.htmlr   django.utils.translationr   rT   django.urlsr   django.utils.safestringr   django.db.modelsr   r   rp   r
   r   r   r   r   r   register
ModelAdminr   rY   TabularInliners   r{   r   r   r   r   sitesite_header
site_titleindex_titler4   r4   r4   r5   <module>   s2    
\` ^iq