from typing import TYPE_CHECKING, List

from core.database import Base
from sqlalchemy import PrimaryKeyConstraint, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

if TYPE_CHECKING:
    from ..code_pedimento_regimens.models import CodePedimentoRegimen


class PedimentoCode(Base):
    __tablename__ = "pedimento_codes"  # GClavePed
    __table_args__ = (
        PrimaryKeyConstraint("code", name="pedimento_codes_pkey"),
        {"schema": "public", "extend_existing": True},  # esquema del anexo 22
    )

    code: Mapped[str] = mapped_column(String(3), nullable=False)
    description: Mapped[str] = mapped_column(String(250), nullable=False)

    # Relación con los regímenes asociados
    # GClavePedRegimen
    regimens: Mapped[List["CodePedimentoRegimen"]] = relationship(
        "CodePedimentoRegimen", uselist=True, back_populates="pedimento"
    )

    def __repr__(self):
        return f"<PedimentoCode(code={self.code}, description={self.description})>"
