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


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


class BusinessDay(Base):
    __tablename__ = "business_days"

    id = Column(Integer, primary_key=True, index=True)
    status = Column(String, default="open", nullable=False)  # 'open' | 'closed'
    opened_at = Column(DateTime(timezone=True), default=_utcnow, nullable=False)
    opened_by_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    closed_at = Column(DateTime(timezone=True), nullable=True)
    closed_by_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    notes = Column(Text, nullable=True)
    # Phase 2E — store-level cash reconciliation
    store_opening_cash = Column(Float, nullable=True)   # physical cash when day opens
    store_closing_cash = Column(Float, nullable=True)   # physical cash counted at close
    store_cash_discrepancy = Column(Float, nullable=True)  # store_closing - expected

    opener = relationship("User", foreign_keys=[opened_by_id])
    closer = relationship("User", foreign_keys=[closed_by_id])
    shifts = relationship("WaiterShift", back_populates="business_day")
