from app.config.extensions import db

# from app.models.spot import Spot
from app.models.brand import Brand
from app.models.base import TimestampedMixin

class Product(TimestampedMixin):
    """
    Represents a product.
    """
    name = db.StringField(
        required=True,  
        help_text="Name of the product."
    )
    poster_image = db.StringField(
        help_text="URL/Path Poster image for the spot."
    )
    brand = db.ReferenceField(
        Brand, 
        help_text="Reference to the product's brand."
    ),  
    # spots = db.ListField(
    #     db.ReferenceField(Spot), 
    #     help_text="References to the spots featuring the product."
    # )

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

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

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

    def to_dict(self):
        """
        Convert the document to a dictionary.
        """ 
        document_dict = super(Product, 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