"""
Modelo ORM para datos específicos de líneas de Activos Fijos - Anexo 24
Extensión de a76.item_lines para SCAF (Sistema de Control de Activo Fijo)
"""

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

if TYPE_CHECKING:
    from api.v1.modules.a76.items.models import LineItem


class FaLineItem(Base, TenantScopedMixin, TimestampMixin):
    """
    Tabla fa_item_lines: Extensión de Anexo 24 para líneas de Activos Fijos.
    Hereda el ID de la tabla item_lines en a76.
    """

    __tablename__ = "fa_item_lines"
    __table_args__ = (
        PrimaryKeyConstraint("id", name="fa_item_lines_pkey"),
        ForeignKeyConstraint(
            ["id"], ["a76.item_lines.id"], name="fk_fa_item_lines_master"
        ),
        {"schema": "a24"},
    )

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

    # Asset information (SCAF specific)
    asset_number: Mapped[Optional[str]] = mapped_column(String(25))  # ASSETNUMBER
    asset_photo: Mapped[Optional[str]] = mapped_column(String(255))  # FOTOACTIVOFIJO
    equipment_message: Mapped[Optional[str]] = mapped_column(String(40))  # EQI_MENSAJE
    invoice_type_asset: Mapped[Optional[str]] = mapped_column(
        String(6)
    )  # TIPOFACTURAASSET
    return_import_invoice: Mapped[Optional[str]] = mapped_column(
        String(15)
    )  # FACTURAIMPORET
    return_import_date: Mapped[Optional[int]] = mapped_column(
        Integer
    )  # FECHAFACIMPORET
    movement_type_import: Mapped[Optional[str]] = mapped_column(
        String(3)
    )  # TIPOMOVIMPO

    # Cross-references IN CASE OF IMPORT REPAIR
    search_invoice: Mapped[Optional[str]] = mapped_column(
        String(15)
    )  # FACTURAEXPO (when line is import) / FACTURAIMPO (when line is export)
    search_line: Mapped[Optional[int]] = mapped_column(
        Integer
    )  # LINEAEXPO (when line is import) / LINEAIMPO (when line is export)

    # Search type
    search_type: Mapped[Optional[str]] = mapped_column(String(10))  # TIPOBUSQUEDA

    # Subitems
    is_subitem: Mapped[Optional[bool]] = mapped_column(Boolean)  # ESSUBPARTIDA
    contains_subitems: Mapped[Optional[bool]] = mapped_column(Boolean)  # CONTIENESUBP
    subitem_number: Mapped[Optional[int]] = mapped_column(Integer)  # SUBPARTIDA

    # Special flags
    discharge: Mapped[Optional[bool]] = mapped_column(Boolean)  # DESCARGA
    own_equipment: Mapped[Optional[bool]] = mapped_column(Boolean)  # EQUIPOPROPIO
    omit_annex31: Mapped[Optional[bool]] = mapped_column(Boolean)  # OMITITENANEXO31

    # --- RELACIÓN ---
    master_info: Mapped["LineItem"] = relationship("LineItem", back_populates="fa_data")

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