from app.config.extensions import db

from app.models.product import Product
from app.models.base import TimestampedMixin


class Spot(TimestampedMixin):  
    
    """
    Represents a spot for advertising.
    """
    name = db.StringField(
        required=True, 
        help_text="Name of the spot."
    )
    duration = db.IntField(
        required=True, 
        help_text="Duration of the spot in seconds."
    )
    products = db.ListField(
        db.ReferenceField(Product), 
        help_text="References to the products featured in the spot."
    )
    cost = db.DecimalField( 
        precision=2,
        min_value=0.0,
        help_text="Cost of producing the spot."
    )
    producer = db.StringField(
        max_length=100,
        help_text="Name of the producer or production company."
    )
    created_date = db.DateTimeField(
        help_text="Date and time when the spot was created."
    )
    is_approved = db.BooleanField(
        default=False,
        help_text="Approval status of the spot."
    )
    cast = db.ListField(
        db.StringField(max_length=100),
        help_text="List of actors or characters featured in the spot."
    )
    poster_image = db.StringField(
        help_text="URL/Path Poster image for the spot."
    )
    spot_video = db.StringField(
        help_text="URL/Path Video file of the spot."
    )
    i_frames = db.ListField(
        db.StringField(),
        help_text="List of URL/Path I-frame images related to the spot."
    )

    meta = {
        'collection': 'spots',  
    }

    def __str__(self):
        return f"< Spot: {self.name} >"

    def __repr__(self):
        return f"< Spot(id={self.id}, name='{self.name}' >"

    def toggle_is_approved(self):
        self.is_approved = not self.is_approved

    def to_dict(self):
        """
        Convert the document to a dictionary.
        """ 
        document_dict = super(Spot, self).to_dict()   
        # Remove any '_cls' field
        document_dict.pop('_cls', 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['created_date'] = self._format_datetime(document_dict.get('created_date')) 
        return document_dict