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

if TYPE_CHECKING:
    from ..models import LineItem

class LineDescription(Base):
    """
    Description details for line items
    Consolidates all line-level data from Q and S tables
    """
    __tablename__ = "item_line_descriptions"
    __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"))
    
    # Descriptions
    description_spanish: Mapped[Optional[str]] = mapped_column(String(4999))  # DESCRIPCIONE
    description_english: Mapped[Optional[str]] = mapped_column(String(4999))  # DESCRIPCIONI
    extra_description: Mapped[Optional[str]] = mapped_column(Text)  # DESCRIPCIONEEXTRA
    part_description: Mapped[Optional[str]] = mapped_column(String(500))  # DESCRIPCIONPARTE
    class_description: Mapped[Optional[str]] = mapped_column(String(500))  # DESCRIPCIONCLASE     
    package_description: Mapped[Optional[str]] = mapped_column(String(500))  # DESCRIPCIONBULTO           
    
    # Product attributes
    brand: Mapped[Optional[str]] = mapped_column(String(50))  # MARCA
    model: Mapped[Optional[str]] = mapped_column(String(50))  # MODELO
    has_serial: Mapped[Optional[bool]] = mapped_column(Boolean)  # LLEVASERIE
    
    # Additional information
    additional_info_spanish: Mapped[Optional[str]] = mapped_column(String(1000))  # INFOADICIONESP
    additional_info_english: Mapped[Optional[str]] = mapped_column(String(1000))  # INFOADICIONING
    
    # Lot and entry tracking
    lot: Mapped[Optional[str]] = mapped_column(String(254))  # LOTE
    entry_number: Mapped[Optional[str]] = mapped_column(String(50))  # NUMENTRADA/NUMERODEENTRADA
    
    # Eighth rule and A31 fields
    eighth_rule_fraction: Mapped[Optional[str]] = mapped_column(String(20))  # Eighth Rule Fraction
    eighth_rule_line: Mapped[Optional[int]] = mapped_column(Integer)  # Eighth Rule Line
    consider_a31: Mapped[Optional[bool]] = mapped_column(Boolean, default=False)  # Consider in A31
    
    # Machinery location
    machinery_location: Mapped[Optional[str]] = mapped_column(String(200))  # Machinery and equipment location
    
    # Relationship (one-to-one)
    line: Mapped["LineItem"] = relationship(back_populates="description")