from decimal import Decimal
from typing import TYPE_CHECKING

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

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


class PedimentoConfigParameters(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "pedimento_config_parameters"
    __table_args__ = (
        PrimaryKeyConstraint("id", name="pedimento_config_parameters_pkey"),        
        ForeignKeyConstraint(
            ["pedimento_id"],
            ["a76.pedimentos.id"],
            ondelete="CASCADE",
            name="fk_pedimento_config_parameters",
        ),
        UniqueConstraint(
            "tenant_id",
            "company_id",
            "pedimento_id",
            name="pedimento_config_parameters_pedimento_id_key",
        ),
        {"schema": "a76"},
    )

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

    is_embassy: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    embassy_dta: Mapped[Decimal] = mapped_column(Numeric(11, 2), default=Decimal('0.00'), server_default='0.00')
    rule_3121_section_ii: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    use_previous_tariff: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    use_payment_date_fi: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    add_state_supplier_record_505: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    customs_value_calculation: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    two_decimals_unit_value: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    customs_value_per_item: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    is_national_supplier: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    is_consolidated: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')

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