from typing import Optional
from decimal import Decimal
from sqlalchemy import Integer, String, ForeignKey, UniqueConstraint, Numeric
from sqlalchemy.orm import Mapped, mapped_column, relationship
from api.v1.common.base_models import TenantScopedMixin, TimestampMixin
from core.database import Base
from api.v1.modules.public.reference_data.currency_types.models import CurrencyType
from api.v1.modules.public.reference_data.countries.models import Country


class MultiCurrencyType(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "multi_currency_types"
    __table_args__ = (
        UniqueConstraint("currency_type_code", "publication_date", "tenant_id", "company_id",
                         name="uq_multi_currency_type_code_date"),
        {"schema": "a76"}
    )

    id: Mapped[int] = mapped_column(
        Integer, primary_key=True, autoincrement=True)

    currency_type_code: Mapped[str] = mapped_column(
        String(3), ForeignKey("public.currency_types.code"), nullable=False)

    country_key: Mapped[Optional[str]] = mapped_column(
        String(3), ForeignKey("public.countries.m3_key"), nullable=True)

    conversion_factor: Mapped[Optional[Decimal]] = mapped_column(
        Numeric(13, 6), nullable=True)
        
    publication_date: Mapped[int] = mapped_column(Integer, nullable=False)

    currency_type: Mapped["CurrencyType"] = relationship()

    country: Mapped[Optional["Country"]] = relationship()
