from app.core.database import Base
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Text
from sqlalchemy.orm import relationship
from datetime import datetime

class Delivery(Base):
    __tablename__ = "deliveries"

    id = Column(Integer, primary_key=True, index=True)
    product_id = Column(Integer, ForeignKey("products.id", ondelete="CASCADE"), nullable=False, index=True)

    amount = Column(Integer, nullable=False, default=0)            # gelieferte Menge (Stück / Einheiten)
    price_cents = Column(Integer, nullable=False, default=0)       # Einkaufspreis in Cent (gesamt oder pro Einheit – je nach Modell)
    delivered_at = Column(DateTime(timezone=True), nullable=True)  # Lieferdatum
    supplier = Column(String, nullable=True)                       # Lieferant (frei)
    invoice_number = Column(String, nullable=True)                 # Rechnungsnummer
    created_by = Column(Integer, nullable=True)                    # optional: User-ID, der die Lieferung erfasst hat
    deposit_return_cents = Column(Integer, nullable=False, server_default="0")
    note = Column(Text, nullable=True)  # oder String(1000)
    
    # Rückverknüpfung zum Produkt
    product = relationship("Product", back_populates="deliveries")
