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


class Reservation(Base):
    __tablename__ = "reservations"

    id = Column(Integer, primary_key=True, index=True)

    # Guest info
    guest_name = Column(String, nullable=False)
    party_size = Column(Integer, nullable=False)
    phone = Column(String, nullable=True)
    email = Column(String, nullable=True)
    note = Column(Text, nullable=True)

    # When the customer wants the table
    reserved_for = Column(DateTime, nullable=False)

    # Optional table assignment (assignable/editable later)
    table_id = Column(Integer, ForeignKey("tables.id"), nullable=True)

    # Lifecycle: pending | arrived | cancelled | no_show
    status = Column(String, nullable=False, default="pending")

    # Source: manager_app | waiter_app | online
    source = Column(String, nullable=False, default="manager_app")

    # Who entered this reservation (staff user)
    created_by_user_id = Column(Integer, ForeignKey("users.id"), nullable=True)

    # For future online reservations: cloud-side customer identifier
    online_customer_id = Column(String, nullable=True)

    created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
    updated_at = Column(DateTime, default=lambda: datetime.now(timezone.utc),
                        onupdate=lambda: datetime.now(timezone.utc))

    table = relationship("Table", foreign_keys=[table_id])
    created_by = relationship("User", foreign_keys=[created_by_user_id])
