from typing import TYPE_CHECKING

from api.v1.common.base_models import TenantScopedMixin, TimestampMixin
from core.database import Base
from sqlalchemy import (
    ForeignKeyConstraint,
    Integer,
    PrimaryKeyConstraint,
    String,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship

if TYPE_CHECKING:
    from api.v1.modules.a76.pedmientos.models.pedimentos import Pedimentos


class PedimentoTransportCarriers(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "pedimento_transport_carriers"
    __table_args__ = (
        PrimaryKeyConstraint("id", name="pedimento_transport_carriers_pkey"),
        ForeignKeyConstraint(
            ["pedimento_id"],
            ["a76.pedimentos.id"],
            ondelete="CASCADE",
            name="fk_pedimento_transport_carriers",
        ),
        {"schema": "a76"},
    )

    id: Mapped[int] = mapped_column(Integer)
    pedimento_id: Mapped[int] = mapped_column(Integer, nullable=False)

    carrier: Mapped[str | None] = mapped_column(String(200), nullable=True)
    rfc: Mapped[str | None] = mapped_column(String(20), nullable=True)
    curp: Mapped[str | None] = mapped_column(String(20), nullable=True)
    name: Mapped[str | None] = mapped_column(String(200), nullable=True)
    address: Mapped[str | None] = mapped_column(String(300), nullable=True)
    city: Mapped[str | None] = mapped_column(String(100), nullable=True)
    state: Mapped[str | None] = mapped_column(String(100), nullable=True)
    country: Mapped[str | None] = mapped_column(String(3), nullable=True)
    tax_id: Mapped[str | None] = mapped_column(String(50), nullable=True)
    total_packages: Mapped[int | None] = mapped_column(Integer, nullable=True)
    identification: Mapped[str | None] = mapped_column(String(100), nullable=True)

    pedimento: Mapped["Pedimentos"] = relationship(
        "Pedimentos", back_populates="pedimento_transport_carriers"
    )
