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


def _utcnow():
    return datetime.now(timezone.utc)


class WasteLog(Base):
    __tablename__ = "waste_log"

    id = Column(Integer, primary_key=True, index=True)
    product_id = Column(Integer, ForeignKey("products.id"), nullable=False)
    quantity = Column(Float, nullable=False)                  # supports decimals
    unit_cost_snapshot = Column(Float, nullable=True)         # product's effective cost at log time
    total_cost = Column(Float, nullable=True)                 # unit_cost_snapshot * quantity
    reason = Column(String, nullable=False)                   # kitchen_error|dropped|expired|other
    reason_notes = Column(Text, nullable=True)
    logged_by_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    business_day_id = Column(Integer, ForeignKey("business_days.id"), nullable=True)
    logged_at = Column(DateTime(timezone=True), default=_utcnow)

    product = relationship("Product", foreign_keys=[product_id])
    logged_by = relationship("User", foreign_keys=[logged_by_id])
