# app/modules/marketplace/models/marketplace_product_translation.py
"""Marketplace Product Translation model for multi-language support.

This model stores localized content (title, description, SEO fields) for
marketplace products. Each marketplace product can have multiple translations
for different languages.
"""

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

from app.core.database import Base
from models.database.base import TimestampMixin


class MarketplaceProductTranslation(Base, TimestampMixin):
    """Localized content for marketplace products.

    Stores translations for product titles, descriptions, and SEO fields.
    Each marketplace_product can have one translation per language.
    """

    __tablename__ = "marketplace_product_translations"

    id = Column(Integer, primary_key=True, index=True)
    marketplace_product_id = Column(
        Integer,
        ForeignKey("marketplace_products.id", ondelete="CASCADE"),
        nullable=False,
    )
    language = Column(String(5), nullable=False)  # 'en', 'fr', 'de', 'lb'

    # === LOCALIZED CONTENT ===
    title = Column(String, nullable=False)
    description = Column(Text)
    short_description = Column(String(500))

    # === SEO FIELDS ===
    meta_title = Column(String(70))
    meta_description = Column(String(160))
    url_slug = Column(String(255))

    # === SOURCE TRACKING ===
    source_import_id = Column(Integer)  # Which import job provided this
    source_file = Column(String)  # e.g., "letzshop_fr.csv"

    # === RELATIONSHIPS ===
    marketplace_product = relationship(
        "MarketplaceProduct",
        back_populates="translations",
    )

    __table_args__ = (
        UniqueConstraint(
            "marketplace_product_id",
            "language",
            name="uq_marketplace_product_translation",
        ),
        Index("idx_mpt_marketplace_product_id", "marketplace_product_id"),
        Index("idx_mpt_language", "language"),
    )

    def __repr__(self):
        return (
            f"<MarketplaceProductTranslation(id={self.id}, "
            f"marketplace_product_id={self.marketplace_product_id}, "
            f"language='{self.language}', "
            f"title='{self.title[:30] if self.title else None}...')>"
        )
