from datetime import date, datetime

from sqlalchemy import Date, DateTime, ForeignKey, Integer, Numeric, String, Text, text
from sqlalchemy.orm import Mapped, mapped_column

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


class Shipment(Base, TenantScopedMixin, TimestampMixin):
    """Operación / Embarque (Diagrama 2). Se crea al liberar una cotización aceptada."""

    __tablename__ = "shipments"
    __table_args__ = {"schema": "ops"}

    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    reference: Mapped[str | None] = mapped_column(String(40), nullable=True, index=True)  # folio de embarque
    case_id: Mapped[int | None] = mapped_column(Integer, ForeignKey("crm.cases.id"), nullable=True, index=True)  # expediente
    quote_id: Mapped[int | None] = mapped_column(
        Integer, ForeignKey("crm.quotes.id"), nullable=True, index=True
    )
    service_request_id: Mapped[int | None] = mapped_column(
        Integer, ForeignKey("crm.service_requests.id"), nullable=True
    )
    account_id: Mapped[int | None] = mapped_column(
        Integer, ForeignKey("crm.accounts.id"), nullable=True, index=True
    )
    operation_type: Mapped[str | None] = mapped_column(String(20), nullable=True)
    transport_mode: Mapped[str | None] = mapped_column(String(20), nullable=True)
    service_type: Mapped[str | None] = mapped_column(String(20), nullable=True)
    incoterm: Mapped[str | None] = mapped_column(String(10), nullable=True)
    origin: Mapped[str | None] = mapped_column(String(160), nullable=True)
    destination: Mapped[str | None] = mapped_column(String(160), nullable=True)
    # abierta | booking | en_transito | arribado | entregada | cerrada | cancelada
    status: Mapped[str] = mapped_column(String(20), nullable=False, server_default=text("'abierta'"), index=True)
    booking_number: Mapped[str | None] = mapped_column(String(60), nullable=True)
    carrier_supplier_id: Mapped[int | None] = mapped_column(
        Integer, ForeignKey("crm.suppliers.id"), nullable=True
    )  # naviera / aerolínea / transportista principal
    ground_carrier_supplier_id: Mapped[int | None] = mapped_column(
        Integer, ForeignKey("crm.suppliers.id"), nullable=True
    )  # transporte terrestre / recolección (R-E-07)
    customs_agent_id: Mapped[int | None] = mapped_column(
        Integer, ForeignKey("crm.suppliers.id"), nullable=True
    )  # agente aduanal
    destination_agent_id: Mapped[int | None] = mapped_column(
        Integer, ForeignKey("crm.suppliers.id"), nullable=True
    )  # agente corresponsal en destino
    cutoff_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)  # Cut Off
    pickup_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)  # cita/ventana de recolección (R-E-07)
    etd: Mapped[date | None] = mapped_column(Date, nullable=True)  # salida estimada
    previous_etd: Mapped[date | None] = mapped_column(Date, nullable=True)  # salida previa tras reprogramación (R-E-06)
    eta: Mapped[date | None] = mapped_column(Date, nullable=True)  # llegada estimada
    vessel_flight: Mapped[str | None] = mapped_column(String(120), nullable=True)  # buque / vuelo
    container_number: Mapped[str | None] = mapped_column(String(60), nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    # ----- Cierre operativo (R-E-22 / disparador de facturación R-F-01) -----
    actual_cost_total: Mapped[float | None] = mapped_column(Numeric(14, 2), nullable=True)  # costos finales reales
    cost_currency: Mapped[str | None] = mapped_column(String(3), nullable=True)
    closed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)  # cierre operativo
    closed_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
    owner_user_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
    created_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
    updated_by: Mapped[str | None] = mapped_column(String(64), nullable=True)


class ShipmentEvent(Base, TenantScopedMixin, TimestampMixin):
    """Hito / bitácora del embarque (Diagramas 2 y 3). Timeline de la operación."""

    __tablename__ = "shipment_events"
    __table_args__ = {"schema": "ops"}

    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    shipment_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("ops.shipments.id"), nullable=False, index=True
    )
    event_type: Mapped[str | None] = mapped_column(String(60), nullable=True)  # clave del hito
    title: Mapped[str] = mapped_column(String(160), nullable=False)
    # hito | decision  — un 'decision' es un punto de decisión del diagrama (rombo)
    kind: Mapped[str] = mapped_column(String(20), nullable=False, server_default=text("'hito'"))
    # pendiente | completado | omitido | rechazado | en_correccion
    status: Mapped[str] = mapped_column(String(20), nullable=False, server_default=text("'pendiente'"))
    # Resultado de un punto de decisión: autorizado | rechazado (NULL mientras está pendiente)
    outcome: Mapped[str | None] = mapped_column(String(20), nullable=True)
    # Ciclo de corrección: el hito de re-trámite apunta a la decisión rechazada que lo originó
    parent_event_id: Mapped[int | None] = mapped_column(
        Integer, ForeignKey("ops.shipment_events.id"), nullable=True
    )
    attempt: Mapped[int] = mapped_column(Integer, nullable=False, server_default=text("1"))  # número de intento
    position: Mapped[int] = mapped_column(Integer, nullable=False, server_default=text("0"))
    planned_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
    actual_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)


class ShipmentDocument(Base, TenantScopedMixin, TimestampMixin):
    """Documento de transporte del embarque (Master/House: MBL, HBL, MAWB, HAWB, CMR, etc.)."""

    __tablename__ = "shipment_documents"
    __table_args__ = {"schema": "ops"}

    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    shipment_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("ops.shipments.id"), nullable=False, index=True
    )
    doc_kind: Mapped[str] = mapped_column(String(10), nullable=False, server_default=text("'otro'"))  # master|house|otro
    # MBL | HBL | MAWB | HAWB | CMR | factura_comercial | packing_list | carta_encomienda | carta_garantia | otro
    doc_type: Mapped[str] = mapped_column(String(30), nullable=False)
    number: Mapped[str | None] = mapped_column(String(80), nullable=True)
    issue_date: Mapped[date | None] = mapped_column(Date, nullable=True)
    file_url: Mapped[str | None] = mapped_column(String(1024), nullable=True)
    file_key: Mapped[str | None] = mapped_column(String(512), nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
