from datetime import datetime
from typing import Optional
from decimal import Decimal
from sqlalchemy import DateTime, ForeignKey, String, Integer, Numeric, Boolean
from sqlalchemy.orm import Mapped, mapped_column
from core.database import Base
from api.v1.common.base_models import TenantScopedMixin, TimestampMixin


class HistoricalTariffFraction(Base, TenantScopedMixin, TimestampMixin):
    """
    Historical tariff fractions catalog.
    Maps to SQL Server table: GFraccionesHistorico
    """

    __tablename__ = "historical_tariff_fractions"
    __table_args__ = {"schema": "a76"}

    id: Mapped[int] = mapped_column(Integer, primary_key=True, nullable=False)
    historical_fraction: Mapped[Optional[str]] = mapped_column(String(8), nullable=True)
    nico: Mapped[Optional[str]] = mapped_column(String(2), nullable=True)
    unit_of_measure_code: Mapped[Optional[str]] = mapped_column(ForeignKey("a76.unit_of_measure_customs.code"), nullable=True)
    country: Mapped[Optional[str]] = mapped_column(ForeignKey("public.countries.m3_key"), nullable=True)
    fraction_type: Mapped[Optional[str]] = mapped_column(String(7), nullable=True)
    sector: Mapped[Optional[str]] = mapped_column(String(5), nullable=True)
    import_tax_rate: Mapped[Optional[Decimal]] = mapped_column(Numeric(7, 2), nullable=True)
    export_tax_rate: Mapped[Optional[Decimal]] = mapped_column(Numeric(7, 2), nullable=True)
    publication_date: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
    is_immex: Mapped[Optional[bool]] = mapped_column(Boolean, nullable=True)
    normal_temporality: Mapped[Optional[bool]] = mapped_column(Boolean, nullable=True)
    services_temporality: Mapped[Optional[bool]] = mapped_column(Boolean, nullable=True)
    certified_temporality: Mapped[Optional[bool]] = mapped_column(Boolean, nullable=True)
    by_log: Mapped[Optional[bool]] = mapped_column(Boolean, nullable=True)
    end_date: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
