from typing import TYPE_CHECKING, Optional

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

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


class PedimentoConfigAdditional(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "pedimento_config_additional"
    __table_args__ = (
        PrimaryKeyConstraint("id", name="pedimento_config_additional_pkey"),
        ForeignKeyConstraint(
            ["pedimento_id"],
            ["a76.pedimentos.id"],
            ondelete="CASCADE",
            name="fk_pedimento_config_additional",
        ),
        UniqueConstraint(
            "tenant_id",
            "company_id",
            "pedimento_id",
            name="pedimento_config_additional_pedimento_id_key",
        ),
        {"schema": "a76"},
    )

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

    add_po_identifier: Mapped[bool] = mapped_column(Boolean)
    do_not_exempt_norms_complement_x: Mapped[bool] = mapped_column(Boolean)
    manual_pedimento_year: Mapped[Optional[int]] = mapped_column(Integer)
    enable_import_invoice_recipient: Mapped[bool] = mapped_column(Boolean)
    send_502_validation_file_for_consolidated: Mapped[bool] = mapped_column(Boolean)
    add_remove_norms: Mapped[bool] = mapped_column(Boolean)

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