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,
    UniqueConstraint,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship

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


class PedimentoRectificationDestination(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "pedimento_rectification_destination"
    __table_args__ = (
        PrimaryKeyConstraint("id", name="pedimento_rectification_destination_pkey"),        
        ForeignKeyConstraint(
            ["pedimento_id"],
            ["a76.pedimentos.id"],
            ondelete="CASCADE",
            name="fk_pedimento_rectification_destination",
        ),
        UniqueConstraint(
            "tenant_id",
            "company_id",
            "pedimento_id",
            name="pedimento_rectification_destination_pedimento_id_key",
        ),
        {"schema": "a76"},
    )

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

    destination_pedimento_year: Mapped[str] = mapped_column(String(2))
    destination_customs_office: Mapped[str] = mapped_column(String(3))
    destination_license: Mapped[str] = mapped_column(String(4))
    destination_pedimento_number: Mapped[str] = mapped_column(String(7))

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