from __future__ import annotations

from datetime import date

from sqlalchemy import Date, Integer, String
from sqlalchemy.orm import Mapped, mapped_column

from api.v1.common.base_models import TenantScopedMixin, TimestampMixin
from core.database import Base


class ExpedienteArchivo(Base, TenantScopedMixin, TimestampMixin):
    """Registro de documentos digitalizados en Ventanilla Única."""

    __tablename__ = "expediente_archivo"
    __table_args__ = ({"schema": "a76"},)

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)

    # Datos del documento (campos del catálogo legacy)
    e_document: Mapped[str | None] = mapped_column(String(50), nullable=True)
    num_operacion: Mapped[str | None] = mapped_column(String(50), nullable=True)
    tipo_documento: Mapped[str | None] = mapped_column(String(10), nullable=True)
    archivo_digitalizado_en: Mapped[str | None] = mapped_column(String(500), nullable=True)
    fecha_digitalizacion: Mapped[date | None] = mapped_column(Date, nullable=True)
    agente_aduanal: Mapped[str | None] = mapped_column(String(50), nullable=True)
    pedimento: Mapped[str | None] = mapped_column(String(21), nullable=True)
    rfc_consulta: Mapped[str | None] = mapped_column(String(13), nullable=True)
    nombre_archivo: Mapped[str | None] = mapped_column(String(255), nullable=True)

    # Estado de la tarea de digitalización
    status: Mapped[str | None] = mapped_column(String(20), nullable=True)  # pending/processing/success/failed
    task_id: Mapped[str | None] = mapped_column(String(255), nullable=True, index=True)
    external_task_id: Mapped[str | None] = mapped_column(String(255), nullable=True, index=True)
    acuse_pdf_path: Mapped[str | None] = mapped_column(String(500), nullable=True)
    envio_xml_path: Mapped[str | None] = mapped_column(String(500), nullable=True)
    respuesta_xml_path: Mapped[str | None] = mapped_column(String(500), nullable=True)
    consulta_envio_xml_path: Mapped[str | None] = mapped_column(String(500), nullable=True)
    consulta_respuesta_xml_path: Mapped[str | None] = mapped_column(String(500), nullable=True)
