from app.config.extensions import db

from app.models.genre import Genre
from app.models.base import TimestampedMixin

class Channel(TimestampedMixin):
    """ 
    TV channel Model

    Represents a TV channel in the application. 

    Attributes:
        name (str): Name of the channel.
        description (str): Description of the channel.
        logo (str): URL of the channel's logo.
        language (str): Language of the channel.
        country (str): Country of the channel.
        genre (List[Genre]): List of references to the TV channel's genre.
        stream_url (str): URL of the channel's feed.
        is_active (bool): Indicates if the channel is active or not.

    Methods:
        to_dict(): Convert the document to a dictionary.

    Meta:
    - collection (str): Specifies the MongoDB collection name for the model.
    """
    
    # Channel name
    name = db.StringField(
        required=True, 
        description='Channel name',
        help_text="Name of the channel."
    )
    # A brief description of the channel
    description = db.StringField(
        description='A brief description of the channel',
        help_text="Description of the channel."
    )
    # URL of the channel's logo
    logo = db.StringField(
        help_text="URL of the channel's logo."
    )
    # Language of the channel
    language = db.StringField(
        help_text="Language of the channel."
    )
    # Country of the channel
    country = db.StringField(
        help_text="Country of the channel."
    )
    # List of genres or categories associated with the channel
    genre = db.ListField(
        db.ReferenceField(Genre), 
        description='List of genres or categories associated with the channel', 
        help_text="List of Reference to the TV channel's genre."
    ) 
    # URL for the channel's content feed
    stream_url =  db.StringField(
        required=True,
        description='URL for the channel\'s content feed',
        help_text="URL of the channel's feed."
    )
    # Indicates if the channel is active
    is_active = db.BooleanField(
        default=True
    )
    # Indicates whether the channel is available in high definition (HD)
    is_hd = db.BooleanField(
        default=False,
        description='Indicates whether the channel is available in high definition (HD)'
    )
    # Website URL for the channel
    website = db.URLField(
        description='Website URL for the channel'
    )
    # Date and time of the channel's launch or establishment
    launch_date = db.DateTimeField(
        description='Date and time of the channel\'s launch or establishment'
    )
    # Location or region where the channel primarily broadcasts
    region = db.StringField(
        description='Location or region where the channel primarily broadcasts'
    )

    def __str__(self):
        return f"TV Channel: {self.name}"

    def __repr__(self):
        return f"< TV Channel(name={self.name}) >"
        
    def toggle_active_status(self):
        self.is_active = not self.is_active
        self.save()

    def to_dict(self):
        """
        Convert the document to a dictionary.
        """ 
        document_dict = super(Channel, self).to_dict()   
        # Remove any '_cls' field
        document_dict.pop('_cls', None)
        # Remove any '_id' field
        document_dict.pop('_id', None)
        # Remove any '_id' field
        document_dict.pop('id', None)
        # replace id field with public id field
        document_dict["id"] =  document_dict.pop('public_id', None)
        # Convert datetime objects to ISO 8601 strings
        document_dict['launch_date'] = self._format_datetime(document_dict.get('launch_date')) 
        # 
        return document_dict    
        
    meta = {
        'collection': 'channels',
        'indexes': [
            {
                'fields': ['$name', '$description'],  # Fields you want to index for text search
                'default_language': 'english',  # Set your preferred language for text search 
            }
        ]
    }