from decimal import Decimal
from typing import Optional, TYPE_CHECKING
from sqlalchemy import Numeric, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from core.database import Base

if TYPE_CHECKING:
    from ..models import LineItem
    
class LineFinancial(Base):
    """
    Financial details for line items
    Consolidates all line-level data from Q and S tables
    """
    __tablename__ = "item_line_financials"
    __table_args__ = {
        "schema": "a76",
    }
    
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    item_line_id: Mapped[int] = mapped_column(ForeignKey("a76.item_lines.id"))

    # Costs - Capture
    unit_cost_capture: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOUNITARIOCAPTURA
    
    # Costs - USD
    unit_cost_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOUNITARIODLLS/COSTOUNITARIOME
    unit_cost_commercial_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOUCOMDLLS
    unit_cost_current_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOACTUALDLLS
    unit_cost_depreciated_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTODEPRECME
    unit_cost_subitem_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOUNITARIOSUBPDLLS
    unit_cost_auxiliary_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOUAUXILIARME
    sales_cost_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOVENTAME
    commercial_unit_cost: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOUNICOMERCIAL
    
    # Costs - MXN
    unit_cost_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOUNITARIOPESOS/COSTOUNITARIOMN
    unit_cost_commercial_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOUCOMPESOS
    unit_cost_current_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOACTUALMN
    unit_cost_depreciated_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTODEPRECMN
    unit_cost_subitem_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOUNITARIOSUBPPESOS
    sales_cost_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # COSTOVENTAMN
    
    # Costs - MC (Custom Currency)
    unit_cost_mc: Mapped[Optional[Decimal]] = mapped_column(Numeric(29, 8))  # COSTOUNITARIOMC
    
    # Values - MXN
    value_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORMN/VALORIMPOMN/VALOREXPOMN
    value_commercial_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALOREXPOCOMMN
    value_updated_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORACTUALIZADOMN
    value_subitem_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORSUBPMN
    sub_import_value_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # SUBVALORIMPOMN
    value_returned_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORRETORNADOMN
    value_depreciated_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORDEPRECIADOMN
    customs_value_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORADUANASMN
    value_total_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORTOTALMN
    value_temp_material_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORMPTEMPMN
    value_def_material_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORMPDEFMN
    value_added_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORAGREMN
    value_national_packing_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALEMPAQUENACMN
    vat_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # IVAIMPOMN/IVAEXPOMN/VALORIVAMN
    vat_used_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORIVAMNUSADO
    advalorem_line_mxn: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # ADVALORMNLINEAPED
    
    # Values - USD
    value_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(29, 8))  # VALORME/VALORIMPOME/VALOREXPOME
    value_commercial_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALOREXPOCOMME
    value_updated_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORACTUALIZADOME
    value_subitem_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORSUBPME
    sub_import_value_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # SUBVALORIMPOME
    value_returned_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORRETORNADOME
    value_depreciated_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORDEPRECIADOME
    customs_value_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORADUANASME
    value_auxiliary_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORIMPOAUXILIARME / VALOREXPORAUXILIARME
    value_total_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORTOTALME
    value_temp_material_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORMPTEMPME
    value_def_material_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORMPDEFME
    value_added_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORAGREME
    value_national_packing_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALEMPAQUENACME
    value_us_packing_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALEMPAQUEUSME
    vat_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # IVAIMPOME/IVAEXPOME/VALORIVAME
    vat_used_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORIVAMEUSADO
    value_non_originating_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORNOORIGINARIOME
    value_originating_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORORIGINARIOME
    igi_amount_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # MONTOIGIME
    exempt_amount_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # MONTOEXCENTOME
    total_commercial_value: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORTOTALCOMERCIAL
    advalorem_line_usd: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # ADVALORMELINEAPED
    
    # Values - MC
    value_mc: Mapped[Optional[Decimal]] = mapped_column(Numeric(29, 8))  # VALORIMPOMC/VALOREXPOMC
    sub_import_value_mc: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # SUBVALORIMPOMC
    vat_mc: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # IVAIMPOMC/IVAEXPOMC
    value_added_mc: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORAGREMC
    value_national_packing_mc: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALEMPAQUENACMC
    value_total_mc: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORTOTALMC
    value_temp_material_mc: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORMPTEMPMC
    value_def_material_mc: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))  # VALORMPDEFMC
    
    # Relationship (one-to-one)
    line: Mapped["LineItem"] = relationship(back_populates="financial")