from datetime import datetime

from sqlalchemy import Boolean, DateTime, Float, String, Text, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column

from app.core.database import Base


class Product(Base):
    __tablename__ = "ordini_products"
    __table_args__ = (
        UniqueConstraint("product_name", "lot_code", "supplier_name", name="uq_ordini_products_identity"),
    )

    id: Mapped[int] = mapped_column(primary_key=True, index=True)
    product_name: Mapped[str] = mapped_column(String(180), nullable=False, index=True)
    lot_code: Mapped[str] = mapped_column(String(120), nullable=False, index=True)
    supplier_name: Mapped[str] = mapped_column(String(180), nullable=False, index=True)
    product_code: Mapped[str | None] = mapped_column(String(120), nullable=True)
    final_price_vat: Mapped[float | None] = mapped_column(Float, nullable=True)
    vat_rate: Mapped[float | None] = mapped_column(Float, nullable=True)
    weight_kg: Mapped[float | None] = mapped_column(Float, nullable=True)
    unit_price_per_kg: Mapped[float | None] = mapped_column(Float, nullable=True)
    category: Mapped[str | None] = mapped_column(String(120), nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    units_per_pack: Mapped[float | None] = mapped_column(Float, nullable=True)
    liters_per_unit: Mapped[float | None] = mapped_column(Float, nullable=True)
    active: Mapped[bool] = mapped_column(Boolean, nullable=False, server_default="true")
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        server_default=func.now(),
        onupdate=func.now(),
        nullable=False,
    )
