from decimal import Decimal
from typing import TYPE_CHECKING, Optional

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

if TYPE_CHECKING:
    from api.v1.modules.a76.manifests.value_manifestation.models import ValueManifestation

class ConceptManifestation(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "concept_manifestations"
    __table_args__ = (
        PrimaryKeyConstraint("value_manifestation_id", "line_number", name="concept_manifestations_pkey"),
        ForeignKeyConstraint(
            ["value_manifestation_id"],
            ["a76.value_manifestations.id"],
            name="fk_concept_manifestation_value_manifestation"
        ),
        Index("idx_concept_manifestations_value_manifestation_id", "value_manifestation_id"),
        {"schema": "a76"},
    )

    value_manifestation_id: Mapped[int] = mapped_column(Integer)
    line_number: Mapped[int] = mapped_column(Integer)
    attachment_type: Mapped[Optional[str]] = mapped_column(String(10), nullable=True)
    number: Mapped[Optional[str]] = mapped_column(String(10), nullable=True)
    merchandise_provider: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
    invoice_document: Mapped[Optional[str]] = mapped_column(String(200), nullable=True)
    amount: Mapped[Optional[Decimal]] = mapped_column(Numeric(19, 9), nullable=True)
    currency: Mapped[Optional[str]] = mapped_column(String(3), nullable=True)
    concept_load: Mapped[Optional[str]] = mapped_column(String(200), nullable=True)

    value_manifestation: Mapped["ValueManifestation"] = relationship(
        "ValueManifestation",
        backref="concepts"
    )
