from sqlalchemy import (
    Column,
    Index,
    Integer,
    String,
)
from sqlalchemy.orm import relationship

# Import Base from the central database module instead of creating a new one
from app.core.database import Base
from models.database.base import TimestampMixin


class MarketplaceProduct(Base, TimestampMixin):
    __tablename__ = "marketplace_products"

    id = Column(Integer, primary_key=True, index=True)
    marketplace_product_id = Column(String, unique=True, index=True, nullable=False)
    title = Column(String, nullable=False)
    description = Column(String)
    link = Column(String)
    image_link = Column(String)
    availability = Column(String, index=True)  # Index for filtering
    price = Column(String)
    brand = Column(String, index=True)  # Index for filtering
    gtin = Column(String, index=True)  # Index for inventory lookups
    mpn = Column(String)
    condition = Column(String)
    adult = Column(String)
    multipack = Column(Integer)
    is_bundle = Column(String)
    age_group = Column(String)
    color = Column(String)
    gender = Column(String)
    material = Column(String)
    pattern = Column(String)
    size = Column(String)
    size_type = Column(String)
    size_system = Column(String)
    item_group_id = Column(String)
    google_product_category = Column(String, index=True)  # Index for filtering
    product_type = Column(String)
    custom_label_0 = Column(String)
    custom_label_1 = Column(String)
    custom_label_2 = Column(String)
    custom_label_3 = Column(String)
    custom_label_4 = Column(String)
    additional_image_link = Column(String)
    sale_price = Column(String)
    unit_pricing_measure = Column(String)
    unit_pricing_base_measure = Column(String)
    identifier_exists = Column(String)
    shipping = Column(String)
    currency = Column(String)

    # New marketplace fields
    marketplace = Column(
        String, index=True, nullable=True, default="Letzshop"
    )  # Index for marketplace filtering
    vendor_name = Column(
        String, index=True, nullable=True
    )  # Index for vendor filtering

    product = relationship("Product", back_populates="marketplace_product")

    # Additional indexes for marketplace queries
    __table_args__ = (
        Index(
            "idx_marketplace_vendor", "marketplace", "vendor_name"
        ),  # Composite index for marketplace+vendor queries
        Index(
            "idx_marketplace_brand", "marketplace", "brand"
        ),  # Composite index for marketplace+brand queries
    )

    def __repr__(self):
        return (
            f"<MarketplaceProduct(marketplace_product_id='{self.marketplace_product_id}', title='{self.title}', marketplace='{self.marketplace}', "
            f"vendor='{self.vendor_name}')>"
        )
