from __future__ import annotations

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

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


class DodaAltaLog(Base, TenantScopedMixin, TimestampMixin):
    """
    Registro histórico de envíos de alta DODA/PITA al servicio externo.
    Cada fila corresponde a un intento de alta para un DODA específico.
    """

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

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

    # Referencia al DODA origen
    doda_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)

    # Tipo de alta (doda / pita)
    variant: Mapped[str | None] = mapped_column(String(10), nullable=True)
    action: Mapped[str | None] = mapped_column(String(20), nullable=True)

    # Datos copiados del DODA al momento del envío (para historial)
    responsible: Mapped[str | None] = mapped_column(String(20), nullable=True)
    patent: Mapped[str | None] = mapped_column(String(10), nullable=True)
    dispatch_customs: Mapped[str | None] = mapped_column(String(10), nullable=True)
    operation_type: Mapped[str | None] = mapped_column(String(5), nullable=True)
    integration_number: Mapped[str | None] = mapped_column(String(50), nullable=True)

    # Respuesta del servicio externo
    task_id: Mapped[str | None] = mapped_column(String(255), nullable=True, index=True)
    status: Mapped[str | None] = mapped_column(String(30), nullable=True)
    message: Mapped[str | None] = mapped_column(String(2000), nullable=True)
    result_json: Mapped[str | None] = mapped_column(Text, nullable=True)
