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 PedimentoValidation(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "pedimento_validation"  # PedimentoValidacion
    __table_args__ = (
        PrimaryKeyConstraint("id", name="pedimento_validation_pkey"),
        ForeignKeyConstraint(
            ["pedimento_id"],
            ["a76.pedimentos.id"],
            ondelete="CASCADE",
            name="fk_pedimento_validation",
        ),
        UniqueConstraint(
            "tenant_id",
            "company_id",
            "pedimento_id",
            name="pedimento_validation_pedimento_id_key",
        ),
        {"schema": "a76"},
    )

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

    validator: Mapped[str] = mapped_column(String(3))  # validador
    validation_ack: Mapped[str] = mapped_column(String(8))  # acuse_validacion
    pre_ack: Mapped[str] = mapped_column(String(8))  # acuse_previo
    line_signature: Mapped[str] = mapped_column(String(50))  # firma_linea_captura
    electronic_signature: Mapped[str] = mapped_column(String(999))  # firma_electronica
    certificate_number: Mapped[str] = mapped_column(String(99))  # numero_certificado
    validator_id: Mapped[int] = mapped_column(Integer)
    responsible_id: Mapped[int] = mapped_column(Integer)

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