import os

from django.utils import timezone
from django.utils.text import slugify



class NoVastResult:
    def __init__(self, id_campaign, total_ads, total_impressions):
        self.id_campaign = id_campaign
        self.total_ads = total_ads
        self.total_impressions = int(total_impressions)


class VastResult:
    def __init__(self, id_campaign, total_ads, total_impressions):
        self.id_campaign = id_campaign
        self.total_ads = total_ads
        self.total_impressions = int(total_impressions)

 
def calculate_priority_score(TSA, ARF, U, P, W_TSA=0.4, W_ARF=0.2, W_U=0.3, W_P=0.1):
    """
    Calculate the priority score for a campaign.
    
    Args:
    - TSA (float): Target Show Alignment value (between 0 and 1).
    - ARF (float): Airing Requirement Fulfillment value (between 0 and 1).
    - U (float): Urgency value (between 0 and 1).
    - P (float): Position value (between 0 and 1).
    - W_TSA (float): Weight for Target Show Alignment (default is 0.4).
    - W_ARF (float): Weight for Airing Requirement Fulfillment (default is 0.2).
    - W_U (float): Weight for Urgency (default is 0.3).
    - W_P (float): Weight for Position (default is 0.1).
    
    Returns:
    - float: Priority score for the campaign.
    """ 
    return (W_TSA * TSA) + (W_ARF * (1 - ARF)) + (W_U * U) + (W_P * P)

 
def hhmmssf_to_seconds(time_str):
    # Split the time string into hours, minutes, seconds, and microseconds
    hours, minutes, seconds, microseconds = map(int, time_str.split(':'))

    # Calculate the total number of seconds
    total_seconds = hours * 3600 + minutes * 60 + seconds + microseconds / 1000000
    
    return total_seconds

 
def get_original_file_upload_path(instance, filename):
    """
    Generate upload path for original files based on channel, brand, and campaign.
    Path format: adspots/original/{channel_name}/{brand_name}/{campaign_name}/{filename}
    Falls back to default structure if any related object is missing.
    """
    # Get the file extension
    name, ext = os.path.splitext(filename) 
    # Create safe filename with timestamp to avoid conflicts
    safe_filename = f"{slugify(name)}_{timezone.now().strftime('%Y%m%d_%H%M%S')}{ext}"
    # Build path components
    path_parts = ['adspots']
    # Add channel name if available
    path_parts.append( slugify(instance.channel.name) if instance.channel and instance.channel.name else 'no-channel')
    # Add brand name if available
    path_parts.append( slugify(instance.brand.name) if instance.brand and hasattr(instance.brand, 'name') and instance.brand.name else 'no-brand')
    # Add campaign name if available
    path_parts.append( slugify(instance.campaign.name) if instance.campaign and hasattr(instance.campaign, 'name') and instance.campaign.name else 'no-campaign')
    # Add year/month for better organization
    path_parts.extend([timezone.now().strftime('%Y'),timezone.now().strftime('%m')])
    # 
    path_parts.append('original')
    # Join all parts and add filename and return full path
    return  os.path.join(*path_parts, safe_filename)


def get_encoded_file_upload_path(instance, filename):
    """
    Generate upload path for encoded files, mirroring the original file structure.
    Path format: adspots/encoded/{channel_name}/{brand_name}/{campaign_name}/{filename}
    """
    # Get the file extension
    name, ext = os.path.splitext(filename) 
    # Create safe filename with timestamp to avoid conflicts
    safe_filename = f"{slugify(name)}_{timezone.now().strftime('%Y%m%d_%H%M%S')}{ext}"
    # Build path components
    path_parts = ['adspots'] 
    # Add channel name if available
    path_parts.append( slugify(instance.channel.name) if instance.channel and instance.channel.name else 'no-channel')
    # Add brand name if available
    path_parts.append( slugify(instance.brand.name) if instance.brand and hasattr(instance.brand, 'name') and instance.brand.name else 'no-brand')
    # Add campaign name if available
    path_parts.append( slugify(instance.campaign.name) if instance.campaign and hasattr(instance.campaign, 'name') and instance.campaign.name else 'no-campaign')
    # Add year/month for better organization
    path_parts.extend([timezone.now().strftime('%Y'),timezone.now().strftime('%m')])
    # 
    path_parts.append('encoded')
    # Join all parts and add filename and return full path
    return  os.path.join(*path_parts, safe_filename)
