# app/modules/marketplace/models/marketplace_import_job.py
"""Marketplace Import Job models for tracking CSV and historical imports."""

from sqlalchemy import JSON, Column, DateTime, ForeignKey, Index, Integer, String, Text
from sqlalchemy.orm import relationship

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


class MarketplaceImportError(Base, TimestampMixin):
    """
    Stores detailed information about individual import errors.

    Each row that fails during import creates an error record with:
    - Row number from the source file
    - Identifier (marketplace_product_id if available)
    - Error type and message
    - Raw row data for review
    """

    __tablename__ = "marketplace_import_errors"

    id = Column(Integer, primary_key=True, index=True)
    import_job_id = Column(
        Integer,
        ForeignKey("marketplace_import_jobs.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )

    # Error location
    row_number = Column(Integer, nullable=False)

    # Identifier from the row (if available)
    identifier = Column(String)  # marketplace_product_id, gtin, mpn, etc.

    # Error details
    error_type = Column(
        String(50), nullable=False
    )  # missing_title, missing_id, parse_error, etc.
    error_message = Column(Text, nullable=False)

    # Raw row data for review (JSON)
    row_data = Column(JSON)

    # Relationship
    import_job = relationship("MarketplaceImportJob", back_populates="errors")

    __table_args__ = (
        Index("idx_import_error_job_id", "import_job_id"),
        Index("idx_import_error_type", "error_type"),
    )

    def __repr__(self):
        return (
            f"<MarketplaceImportError(id={self.id}, job_id={self.import_job_id}, "
            f"row={self.row_number}, type='{self.error_type}')>"
        )


class MarketplaceImportJob(Base, TimestampMixin):
    __tablename__ = "marketplace_import_jobs"

    id = Column(Integer, primary_key=True, index=True)
    store_id = Column(Integer, ForeignKey("stores.id"), nullable=False, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)

    # Import configuration
    marketplace = Column(String, nullable=False, index=True, default="Letzshop")
    source_url = Column(String, nullable=False)
    language = Column(
        String(5), nullable=False, default="en"
    )  # Language for translations

    # Status tracking
    status = Column(
        String, nullable=False, default="pending"
    )  # pending, processing, completed, failed, completed_with_errors

    # Results
    imported_count = Column(Integer, default=0)
    updated_count = Column(Integer, default=0)
    error_count = Column(Integer, default=0)
    total_processed = Column(Integer, default=0)

    # Error handling
    error_message = Column(Text)

    # Celery task tracking (optional - for USE_CELERY=true)
    celery_task_id = Column(String(255), nullable=True, index=True)

    # Timestamps
    started_at = Column(DateTime(timezone=True))
    completed_at = Column(DateTime(timezone=True))

    # Relationships
    # NOTE: No back_populates - optional modules own relationships to core models
    # Core models (User, Store) don't have back-references to optional modules
    store = relationship("Store")
    user = relationship("User", foreign_keys=[user_id])
    errors = relationship(
        "MarketplaceImportError",
        back_populates="import_job",
        cascade="all, delete-orphan",
        order_by="MarketplaceImportError.row_number",
    )

    # Indexes for performance
    __table_args__ = (
        Index("idx_import_store_status", "store_id", "status"),
        Index("idx_import_store_created", "store_id", "created_at"),
        Index("idx_import_user_marketplace", "user_id", "marketplace"),
    )

    def __repr__(self):
        return (
            f"<MarketplaceImportJob(id={self.id}, store_id={self.store_id}, "
            f"marketplace='{self.marketplace}', status='{self.status}', "
            f"imported={self.imported_count})>"
        )
