from sqlalchemy import Column, Integer, String, Boolean, DateTime
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.core.database import Base

class Product(Base):
    __tablename__ = "products"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, nullable=False, unique=True, index=True)
    category = Column(String, nullable=True, index=True)
    volume_ml = Column(Integer, nullable=False)
    price_cents = Column(Integer, nullable=False)
    supplier_number = Column(String, nullable=True)
    image_url = Column(String, nullable=True)

    stock = Column(Integer, nullable=False, default=0)
    is_active = Column(Boolean, default=True)

    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    pack_size = Column(Integer, nullable=False, default=1)             # z.B. 6, 12, 24
    purchase_price_cents = Column(Integer, nullable=False, default=0) 
    # >>> NEU: Gegenbeziehung zur Delivery-Relation
    deliveries = relationship(
        "Delivery",
        back_populates="product",
        cascade="all, delete-orphan",
        lazy="selectin",
    )
