"""
Modelo ORM para datos específicos de Activos Fijos (Q-Partes) - Anexo 24
"""

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

if TYPE_CHECKING:
    
    from api.v1.modules.a76.parts.models import Part 


class FaPart(Base, TenantScopedMixin, TimestampMixin):
    """
    Tabla fa_partes: Extensión de Anexo 24 para Activos Fijos.
    """

    __tablename__ = "fa_partes"
    __table_args__ = (
        PrimaryKeyConstraint("id", name="fa_partes_pkey"),
        ForeignKeyConstraint(
            ["id"], ["a76.parts.id"], name="fk_fa_partes_master"
        ),
        {"schema": "a24", "extend_existing": True},
    )

    # El ID hereda el valor de la tabla parts
    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=False)

    # --- CAMPOS ESPECÍFICOS FISCALES (Q-PARTES) ---
    origin_country: Mapped[Optional[str]] = mapped_column(String(3)) # PAIS
    sector: Mapped[Optional[str]] = mapped_column(String(8))         # SECTOR
    fraction_type: Mapped[Optional[str]] = mapped_column(String(7))  # TIPOFRACCION

    # --- RELACIÓN ---
    # Usamos string "Part" para evitar que truene al inicializar los mappers
    master_info: Mapped["Part"] = relationship("Part", back_populates="fa_data")

    def __repr__(self) -> str:
        return f"<FaPart(id={self.id}, sector='{self.sector}')>"