from typing import TYPE_CHECKING, Optional

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

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


class PedimentoConfigCalculations(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "pedimento_config_calculations"
    __table_args__ = (
        PrimaryKeyConstraint("id", name="pedimento_config_calculations_pkey"),
        ForeignKeyConstraint(
            ["pedimento_id"],
            ["a76.pedimentos.id"],
            ondelete="CASCADE",
            name="fk_pedimento_config_calculations",
        ),
        UniqueConstraint(
            "tenant_id",
            "company_id",
            "pedimento_id",
            name="pedimento_config_calculations_pedimento_id_key",
        ),
        {"schema": "a76"},
    )

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

    dta_type: Mapped[Optional[str]] = mapped_column(String(1), nullable=True)
    dta_operation: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    dta_vehicle_count: Mapped[int] = mapped_column(SmallInteger, default=0, server_default='0')
    dta_mixed_rate_8permil: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    pays_vat: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    pays_prevalidation: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    include_sagar_certificate_fee: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    fixed_vehicle_dta_fee: Mapped[bool] = mapped_column(Boolean, default=False, server_default='false')
    additional_fixed_fee: Mapped[int] = mapped_column(SmallInteger, default=0, server_default='0')
    additional_fixed_fee_payment_method: Mapped[Optional[int]] = mapped_column(SmallInteger, nullable=True)

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