from django.db import models 


from apps.common.models import BaseModel
from apps.channels.models.channels import Channel


class AdbreakType(models.TextChoices):
    Pre = 'Pre'
    Post = 'Post'
    Mid = 'Mid'

class AdbreakCategory(models.TextChoices):
    History = 'History'
    Predicted = 'Predicted'
    RealTime = 'RealTime'
    Conductor = 'Conductor'

    

class Adbreak(BaseModel): 
    channel = models.ForeignKey(
        Channel, 
        on_delete=models.SET_NULL, 
        blank=True, 
        null=True
    )

    date = models.DateField(
        verbose_name="Date",
        help_text="The date of the ad-break."
    )

    start_at = models.TimeField(
        verbose_name="Ad Break Start Time",
        help_text="The start time of the ad break.",
        blank=True, 
        null=True
    )

    end_at = models.TimeField(
        verbose_name="Ad Break End Time",
        help_text="The end time of the ad break.",
        blank=True, 
        null=True
    ) 

    duration = models.DurationField( 
        verbose_name="Duration",
        blank=True,
        null=True,
        help_text="The duration of the ad break."
    )
    
    adbreak_type = models.CharField(
        max_length=255,
        choices=AdbreakType.choices,
        blank=True,
        null=True
    )

    adbreak_category = models.CharField(
        max_length=255,
        choices=AdbreakCategory.choices,
        blank=True,
        null=True
    )
    
    show_before = models.CharField(
        max_length=255,
        verbose_name="Show Before",
        blank=True,
        null=True,
        help_text="The show before the ad break."
    ) 
    show_after = models.CharField(
        max_length=255,
        verbose_name="Show After",
        blank=True,
        null=True,
        help_text="The show after the ad break."
    ) 
    if_show_during = models.BooleanField(
        default=True,
        verbose_name="Show During",
        help_text="Boolean indicating if the ad break is shown during a show."
    )

    class Meta: 
        db_table = 'Adbreaks'
        verbose_name_plural = "Conductor Data"
        indexes = [models.Index(fields=['start_at'], name='idx_start_at'),
                   models.Index(fields=['end_at'], name='idx_end_at'),]

    def save(self, *args, **kwargs):
        if not self.date:
            # If date is not set, set it to yesterday's date
            self.date = timezone.now().date() - timezone.timedelta(days=1)
        super().save(*args, **kwargs)


 

 