"""
SQLAlchemy ORM models for card import tracking.

This module provides models for tracking card import batches
and individual card import records.
"""
from sqlalchemy import (
    Column, Integer, String, BigInteger, Boolean, DateTime, Text,
    ForeignKey, UniqueConstraint
)
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.core.database import Base


class CardImportBatch(Base):
    """
    Card import batch tracking.
    
    Tracks a batch of card imports from a file, including
    matching results and error information.
    """
    __tablename__ = "card_import_batches"
    
    id = Column(BigInteger, primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey("mtgonline_users.id", ondelete="CASCADE"), nullable=False)
    filename = Column(String(255), nullable=False)
    file_type = Column(String(50), nullable=True)  # 'mtgjson', 'csv', etc.
    file_size = Column(BigInteger, nullable=True)  # Size in bytes
    status = Column(String(20), default="PENDING")  # PENDING, PROCESSING, COMPLETED, FAILED
    total_cards = Column(Integer, default=0)
    matched_cards = Column(Integer, default=0)
    unmatched_cards = Column(Integer, default=0)
    match_results = Column(Text, nullable=True)  # JSON of match details
    error_message = Column(Text, nullable=True)
    created_at = Column(DateTime, server_default=func.now())
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
    
    # Relationships
    user = relationship("User", backref="card_import_batches")
    records = relationship(
        "UserCardImportRecord",
        back_populates="batch",
        cascade="all, delete-orphan"
    )
    
    def __repr__(self) -> str:
        return f"<CardImportBatch {self.filename} (status={self.status})>"


class UserCardImportRecord(Base):
    """
    Individual card import record within a batch.
    
    Tracks whether each card in an import batch was confirmed
    and when the confirmation happened.
    """
    __tablename__ = "user_card_imports_confirmed"
    
    id = Column(BigInteger, primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey("mtgonline_users.id", ondelete="CASCADE"), nullable=False)
    batch_id = Column(BigInteger, ForeignKey("card_import_batches.id", ondelete="CASCADE"), nullable=False)
    is_confirmed = Column(Boolean, default=False)
    confirmed_at = Column(DateTime, nullable=True)
    
    # Relationships
    batch = relationship("CardImportBatch", back_populates="records")
    
    __table_args__ = (
        UniqueConstraint('user_id', 'batch_id', name='uq_user_card_import_batch'),
    )
    
    def __repr__(self) -> str:
        return f"<UserCardImportRecord user={self.user_id} batch={self.batch_id} confirmed={self.is_confirmed}>"
