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

if TYPE_CHECKING:
    from ..models import LineItem

class LineReference(Base):
    """
    Reference details for line items
    Consolidates all line-level data from Q and S tables
    """
    __tablename__ = "item_line_references"
    __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"))
      
    serie_id: Mapped[Optional[int]] = mapped_column(ForeignKey("a76.item_line_series.id"))  # SERIEPARTIDA  
      
    # Customer/Vendor
    customer_invoice: Mapped[Optional[str]] = mapped_column(ForeignKey("a76.clients_and_providers.id"))  # CLIENTEFACTURAR
    assigned_client: Mapped[Optional[str]] = mapped_column(ForeignKey("a76.clients_and_providers.id"))  # CLIENTEASIGNADO
    supplier: Mapped[Optional[str]] = mapped_column(ForeignKey("a76.clients_and_providers.id"))  # PROVEEDOR
    requisitioner: Mapped[Optional[str]] = mapped_column(ForeignKey("a76.clients_and_providers.id"))  # REQUISITOR
    sent_to: Mapped[Optional[str]] = mapped_column(ForeignKey("a76.clients_and_providers.id"))  # ENVIADOA
    
    # PED line reference
    ped_line: Mapped[Optional[int]] = mapped_column(Integer)  # LINEAPED
    ro_line: Mapped[Optional[int]] = mapped_column(Integer)  # LINEARO
    
    # Relationship (one-to-one)
    line: Mapped["LineItem"] = relationship(back_populates="reference")