o
    yeh\-                     @   s   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZ e ZG d	d
 d
eeeZG dd deZG dd deeeZG dd deeeZdS )aa  Advertisers Models

This module defines the database models for advertiser and brand management.
It includes models for agencies, brands, and their relationships.

Models:
    - Agency: Advertising agency information
    - Brand: Brand/advertiser information
    - BrandCategory: Brand categorization
    - UserAdvertiser: User-advertiser relationships
    )models)get_user_model)EmailValidatorURLValidator)timezone)gettext_lazy)	BaseModel
AuditModelStatusModel)agencies_logo_upload_pathc                   @   s  e Zd ZdZejddddZejddddZej	e
 gdd	Zejd
ddddZejdde gddZejddddZejdddddZejdddddZejeddddZejdddddZejeejdddddZG dd dZdd Zedd Zedd  Zd!S )"Agencya  Advertising Agency model.
    
    Represents advertising agencies that manage multiple brands
    and campaigns. Agencies can have multiple users and brands
    associated with them.
    
    Attributes:
        name (str): Agency name
        description (str): Agency description
        email (str): Primary contact email
        phone (str): Primary contact phone
        website (str): Agency website URL
        address (str): Physical address
        city (str): City location
        country (str): Country location
        logo (ImageField): Agency logo
        contact_person (str): Primary contact person name
        is_active (bool): Whether agency is active
       TzAgency name (must be unique)
max_lengthunique	help_textz"Detailed description of the agencyblanknullr   z$Primary contact email for the agency)
validatorsr      zPrimary contact phone numberr   r   r   r   zAgency website URLr   r   r   r   zPhysical address of the agencyd   zCity where agency is locatedzCountry where agency is locatedzAgency logo image	upload_tor   r   r   z$Primary contact person at the agencyZowned_agenciesz!User who owns/manages this agency	on_deleter   r   related_namer   c                   @   sF   e Zd ZdZdZdZdgZejdgdejdgdejdgdgZ	dS )	zAgency.MetaZagenciesr   ZAgenciesnamefieldsstatus
created_atN)
__name__
__module____qualname__db_tableverbose_nameverbose_name_pluralorderingr   Indexindexes r-   r-   KC:\Users\vibe-look\OneDrive\Desktop\Adtlas_V\src\apps\advertisers\models.pyMetaz   s    r/   c                 C   s   | j S )N)r   selfr-   r-   r.   __str__   s   zAgency.__str__c                 C      | j jdd S )z7Get total number of brands associated with this agency.activer"   N)brandsfiltercountr0   r-   r-   r.   total_brands      zAgency.total_brandsc                 C   s    ddl m} |jj| dd S )z7Get total number of campaigns for this agency's brands.r   Campaignr4   )Zadvertiser__agencyr"   N)apps.campaigns.modelsr<   objectsr7   r8   )r1   r<   r-   r-   r.   total_campaigns   s   zAgency.total_campaignsN) r$   r%   r&   __doc__r   	CharFieldr   	TextFielddescription
EmailFieldr   emailphoneURLFieldr   websiteaddresscitycountry
ImageFieldr   logoZcontact_person
ForeignKeyUserSET_NULLownerr/   r2   propertyr9   r?   r-   r-   r-   r.   r      s    	
r   c                   @   s^   e Zd ZdZejddddZejddddZej	dej
ddd	d
dZG dd dZdd ZdS )BrandCategoryaF  Brand Category model for organizing brands.
    
    Provides categorization for brands to help with organization
    and filtering in the system.
    
    Attributes:
        name (str): Category name
        description (str): Category description
        parent (ForeignKey): Parent category for hierarchical structure
    r   TzCategory name (must be unique)r   zDescription of the categoryr   r1   Zsubcategoriesz-Parent category for hierarchical organizationr   c                   @   s   e Zd ZdZdZdZdgZdS )zBrandCategory.MetaZbrand_categorieszBrand CategoryzBrand Categoriesr   N)r$   r%   r&   r'   r(   r)   r*   r-   r-   r-   r.   r/      s
    
r/   c                 C   s    | j r| j j d| j S | jS )Nz > )parentr   r0   r-   r-   r.   r2      s   zBrandCategory.__str__N)r$   r%   r&   r@   r   rA   r   rB   rC   rN   CASCADErT   r/   r2   r-   r-   r-   r.   rS      s,    	rS   c                   @   s  e Zd ZdZejdddZejddddZej	e
ejdddd	d
Zej	eejdddZejdddddZejdde gddZejdde gddZejdddddZejdddddZejddddZejddddddZG dd dZdd  Zed!d" Z ed#d$ Z!ed%d& Z"d'S )(Branda  Brand/Advertiser model.
    
    Represents brands or advertisers that create advertising campaigns.
    Brands are associated with agencies and can have multiple campaigns.
    
    Attributes:
        name (str): Brand name
        description (str): Brand description
        category (ForeignKey): Brand category
        agency (ForeignKey): Associated agency
        logo (ImageField): Brand logo
        website (str): Brand website
        contact_email (str): Brand contact email
        contact_phone (str): Brand contact phone
        industry (str): Industry sector
        target_audience (str): Primary target audience
        is_active (bool): Whether brand is active
    r   zBrand or advertiser name)r   r   Tz!Detailed description of the brandr   r6   zBrand category for organizationr   zAgency managing this brandr   r   r   zbrands/logos/zBrand logo imager   zBrand website URLr   zBrand contact emailr   zBrand contact phone numberr   r   z3Industry sector (e.g., Automotive, Food & Beverage)z&Description of primary target audience      zAnnual advertising budget)
max_digitsdecimal_placesr   r   r   c                   @   sr   e Zd ZdZdZdZdgZejdgdejdgdejdgdejdgdejd	gdgZ	ej
ddgd
dgZdS )z
Brand.Metar6   rV   ZBrandsr   r    agencycategoryr"   r#   Zunique_brand_per_agencyr!   r   N)r$   r%   r&   r'   r(   r)   r*   r   r+   r,   UniqueConstraintconstraintsr-   r-   r-   r.   r/   '  s     r/   c                 C   s   | j  d| jj  dS )N ())r   r\   r0   r-   r-   r.   r2   :  s   zBrand.__str__c                 C   r3   )z-Get total number of campaigns for this brand.r4   r5   N)	campaignsr7   r8   r0   r-   r-   r.   r?   =  r:   zBrand.total_campaignsc                 C   s&   ddl m} t }| jjd||dS )z.Get currently active campaigns for this brand.r   r;   r4   )r"   Zstart_date__lteZend_date__gteN)r=   r<   r   nowrc   r7   )r1   r<   rd   r-   r-   r.   active_campaignsB  s   zBrand.active_campaignsc                 C   s6   ddl m} ddlm} | jj|dd}|d pdS )z2Calculate total budget spent across all campaigns.r   r;   )SumZbudget)totalrg   N)r=   r<   django.db.modelsrf   rc   	aggregate)r1   r<   rf   resultr-   r-   r.   total_budget_spentM  s   zBrand.total_budget_spentN)#r$   r%   r&   r@   r   rA   r   rB   rC   rN   rS   rP   r]   r   rU   r\   rL   rM   rG   r   rH   rD   r   Zcontact_emailZcontact_phoneZindustryZtarget_audienceDecimalFieldZannual_budgetr/   r2   rR   r?   re   rk   r-   r-   r-   r.   rV      s    	


rV   c                   @   s   e Zd ZdZg dZejeejdddZ	eje
ejdddZejded	d
dZejedddZejdddZG dd dZdd Zdd ZdS )UserAdvertisera  User-Advertiser relationship model.
    
    Manages the many-to-many relationship between users and advertisers/brands.
    Allows users to have access to specific brands with different permission levels.
    
    Attributes:
        user (ForeignKey): User account
        brand (ForeignKey): Associated brand
        role (str): User's role for this brand
        permissions (str): Specific permissions
        is_active (bool): Whether relationship is active
    ))viewerZViewer)editorEditor)managerManager)adminZAdministratorZbrand_relationshipszUser accountrW   Zuser_relationshipszAssociated brandr   rn   zUser's role for this brand)r   choicesdefaultr   Tz5Specific permissions for this user-brand relationship)ru   r   r   z-Whether this relationship is currently active)ru   r   c                   @   s4   e Zd ZdZdZdZddgZejddgdd	gZ	d
S )zUserAdvertiser.MetaZuser_advertiserszUser-Advertiser RelationshipzUser-Advertiser RelationshipsZuser__usernameZbrand__nameuserbrandZunique_user_brand_relationshipr^   N)
r$   r%   r&   r'   r(   r)   r*   r   r_   r`   r-   r-   r-   r.   r/     s    r/   c                 C   s    | j j d| jj d| j dS )Nz - ra   rb   )rv   usernamerw   r   roler0   r-   r-   r.   r2     s    zUserAdvertiser.__str__c                 C   s    | j dkrdS || jdg v S )z5Check if user has specific permission for this brand.rs   TallowedN)ry   permissionsget)r1   
permissionr-   r-   r.   has_permission  s   
zUserAdvertiser.has_permissionN)r$   r%   r&   r@   ZROLE_CHOICESr   rN   rO   rU   rv   rV   rw   rA   ry   	JSONFielddictr{   BooleanField	is_activer/   r2   r~   r-   r-   r-   r.   rm   Y  sB    rm   N)r@   	django.dbr   django.contrib.authr   django.core.validatorsr   r   django.utilsr   django.utils.translationr   _Zapps.common.modelsr   r	   r
   Zapps.advertisers.utilsr   rO   r   rS   rV   rm   r-   r-   r-   r.   <module>   s    }- 