"""Pydantic schemas for card search and import features."""
from pydantic import BaseModel, Field, ConfigDict
from typing import List, Optional, Dict, Any
from datetime import datetime


# ===== Card Search Schemas =====

class CardResponse(BaseModel):
    """Card response with details."""
    id: int
    name: str
    mana_cost: Optional[str] = None
    type_line: Optional[str] = None
    oracle_text: Optional[str] = None
    power: Optional[str] = None
    toughness: Optional[str] = None
    rarity: Optional[str] = None
    layout: Optional[str] = None
    colors: Optional[str] = None
    set_code: Optional[str] = None
    set_name: Optional[str] = None
    identifiers: Optional[Dict[str, Any]] = None
    images: Optional[Dict[str, Any]] = None

    model_config = ConfigDict(from_attributes=True)


class SetResponse(BaseModel):
    """Set response."""
    id: int
    name: str
    code: str
    release_date: Optional[datetime] = None
    card_count: Optional[int] = None

    model_config = ConfigDict(from_attributes=True)


class CardTypeResponse(BaseModel):
    """Card type response."""
    type: str


class CardSearchResponse(BaseModel):
    """Card search response."""
    cards: List[Dict[str, Any]]
    total: int
    page: int
    page_size: int
    total_pages: int


# ===== Card Import Schemas =====

class CardImportResponse(BaseModel):
    """Response after successful card import upload."""
    message: str
    batch_id: int
    card_count: int
    status: str
    imported_at: datetime


class CardImportStatusResponse(BaseModel):
    """Response showing current import status."""
    has_import: bool
    batch_id: Optional[int] = None
    status: Optional[str] = None
    card_count: Optional[int] = None
    matched_count: Optional[int] = None
    unmatched_count: Optional[int] = None
    last_imported: Optional[datetime] = None
    error_message: Optional[str] = None


class CardMatchResult(BaseModel):
    """Result of matching imported card name to database card."""
    card_id: Optional[int] = None
    card_name: str
    matched_name: str
    match_type: str  # 'exact', 'high_confidence', 'low_confidence'
    confidence: float  # 0.0 to 1.0


class CardImportSummary(BaseModel):
    """Summary of card import with match results."""
    total_cards: int
    matched_cards: List[CardMatchResult]
    unmatched_cards: List[str]
    import_id: Optional[int] = None


# Generic response models
class MessageResponse(BaseModel):
    """Generic message response."""
    message: str


class CountResponse(BaseModel):
    """Generic count response."""
    count: int


class ErrorResponse(BaseModel):
    """Error response with details."""
    detail: str
    error_code: Optional[str] = None
