import pytest
import factory
from app.models.brand import Brand
from app.models.base import UniqueIDMixin
from app.models.genre import Genre
from app.models.channel import Channel

# from app.models.product import Product

import json

class UniqueIDMixinFactory(factory.mongoengine.MongoEngineFactory):
    class Meta:
        model = UniqueIDMixin

    # You can customize the attributes based on your needs
    public_id = factory.Faker('uuid4')  # Use Faker to generate a random UUID

    # You might need to adjust this part based on your model's constructor logic
    def __init__(self, *args, **kwargs):
        super(UniqueIDMixinFactory, self).__init__(*args, **kwargs)
        if not self.public_id:
            self.generate_public_id()

    @classmethod
    def _create(cls, model_class, *args, **kwargs):
        """
        Override the default _create method to avoid calling the constructor.

        This is necessary because the constructor logic has been included in the __init__
        method of the model, so calling it again here would lead to duplicate public_id generation.

        """
        instance = model_class(*args, **kwargs)
        instance.save()
        return instance

# class ProductFactory(factory.mongoengine.MongoEngineFactory):
#     class Meta:
#         model = Product
#
#     name = factory.Faker('word')  # Use Faker to generate a random word for the product name
#     poster_image = factory.Faker('image_url')  # Use Faker to generate a random image URL
#     # brand = factory.SubFactory(BrandFactory)  # Assuming you have a BrandFactory defined
#     # spots = factory.SubFactoryList(SpotFactory, size=3)

class BrandFactory(factory.mongoengine.MongoEngineFactory):
    class Meta:
        model = Brand

    name = factory.Faker('company')  # Use Faker to generate a random company name
    parent_brand = None  # Set to None or provide logic to create a parent brand if needed
    products = "Test"  # This should be subclass of product object
    description = factory.Faker('sentence')
    logo_url = factory.Faker('image_url')
    country = factory.Faker('country')
    website = factory.Faker('url')
    founded_date = factory.Faker('date_this_century', before_today=True)

    @classmethod
    def to_dict(cls):
        instance = cls.build()
        return dict(instance)

    @classmethod
    def to_json(cls):
        instance = cls.build()
        return json.dumps(instance)




class GenreFactory(factory.mongoengine.MongoEngineFactory):
    class Meta:
        model = Genre

    name = factory.Faker('word')  # Adjust based on your requirements
    description = factory.Faker('sentence')
    category = factory.Faker('random_element', elements=["Channel", "Program"])

class ChannelFactory(factory.mongoengine.MongoEngineFactory):
    class Meta:
        model = Channel

    name = factory.Faker('company')  # You can use Faker for generating fake data
    description = factory.Faker('sentence')
    logo = factory.Faker('image_url')
    language = "arabic"
    country = factory.Faker('country_code')
    genre = factory.List([factory.SubFactory(GenreFactory)])
    stream_url = factory.Faker('url')
    is_active = factory.Faker('boolean')
    is_hd = factory.Faker('boolean')
    website = factory.Faker('url')
    launch_date = factory.Faker('date_time')
    region = factory.Faker('city')
