from sqlalchemy import Float, ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.database import Base


class OrderItem(Base):
    __tablename__ = "ordini_items"

    id: Mapped[int] = mapped_column(primary_key=True, index=True)
    batch_id: Mapped[int] = mapped_column(ForeignKey("ordini_batches.id", ondelete="CASCADE"), nullable=False, index=True)
    product_id: Mapped[int | None] = mapped_column(ForeignKey("ordini_products.id", ondelete="SET NULL"), nullable=True, index=True)
    product_name: Mapped[str] = mapped_column(String(180), nullable=False)
    lot_code: Mapped[str] = mapped_column(String(120), nullable=False)
    supplier_name: Mapped[str] = mapped_column(String(180), nullable=False)
    quantity: Mapped[int] = mapped_column(Integer, nullable=False)
    final_price_vat_snapshot: Mapped[float | None] = mapped_column(Float, nullable=True)
    estimated_line_total: Mapped[float | None] = mapped_column(Float, nullable=True)
    units_per_pack: Mapped[float | None] = mapped_column(Float, nullable=True)
    liters_per_unit: Mapped[float | None] = mapped_column(Float, nullable=True)

    batch = relationship("OrderBatch", back_populates="items")
    product = relationship("Product")
