from app.config.extensions import db

from app.models.genre import Genre
from app.models.base import TimestampedMixin
from flask_restx import Namespace, Resource, fields, Model


class Program(TimestampedMixin):
    """
    TV program Model

    Represents a TV program in the application. 

    Attributes:
        name (str): Name of the program.
        summary (str): A summary or description of the program.
        language (str): Language of the program.
        genre (List[Genre]): List of references to the genres associated with the program.
        officialSite (str): The official URL of the program's page.
        program_type (str): The type of the program. Choices: "scripted", "reality", "comedy", "news".
        status (str): The status of the program. Choices: "ongoing", "ended", "upcoming".
        runtime (int): The duration of each episode in minutes.
        premiered (datetime.date): The date when the program first premiered.
        image (dict): The URLs of the program's images (medium and original sizes).

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

    Meta:
        collection (str): The name of the MongoDB collection to store the documents.
    """

    # Program name
    name = db.StringField(
        required=True, 
        description='Program name',
        help_text="Name of the program."
    )
    # Summary or Description of the program
    summary = db.StringField(
        help_text="A summary or description of the program."
    )
    language = db.StringField(
        help_text="Language of the channel."
    )
    # Genre or category of the program
    genre = db.ListField(
        db.ReferenceField(Genre), 
        help_text="List of Reference to The genres associated with the program.."
    )  
    officialSite = db.StringField(
        help_text="The official URL of the program's page.", 
    ) 
    program_type = db.StringField(
        choices=["scripted", "reality", "comedy", "news"],
        help_text="The type of the program."
    )  
    status = db.StringField(
        choices=["ongoing", "ended", "upcoming"],
        help_text="The status of the program."
    )
    # Duration of the program in minutes
    runtime = db.IntField(
        help_text="The duration of each episode in minutes."
    )
    # Release date of the program
    premiered = db.DateField(
        help_text="The date when the program first premiered."
    ) 
    # Image field for the program's poster
    image = db.DictField(
        help_text="The URLs of the program's images (medium and original sizes)."
    )

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

    def __repr__(self):
        return f"< TV Program(name={self.name}, genre={self.genre.to_dict()}) >"
        
    def to_dict(self):
        """
        Convert the document to a dictionary.
        """ 
        document_dict = super(Program, 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['premiered'] = self._format_datetime(document_dict.get('premiered'))
        # 
        return document_dict  
        
    meta = {
        'collection': 'programs'
    }
    
