from sqlalchemy import Column, Integer, String, Text, Enum, ForeignKey, DateTime, Boolean
from sqlalchemy.orm import relationship
from datetime import datetime
import enum
from ...shared.config.database import Base

class RequestType(str, enum.Enum):
    extra_towels = 'extra_towels'
    extra_pillows = 'extra_pillows'
    room_cleaning = 'room_cleaning'
    turndown_service = 'turndown_service'
    amenities = 'amenities'
    maintenance = 'maintenance'
    room_service = 'room_service'
    other = 'other'

class RequestStatus(str, enum.Enum):
    pending = 'pending'
    in_progress = 'in_progress'
    fulfilled = 'fulfilled'
    cancelled = 'cancelled'

class RequestPriority(str, enum.Enum):
    low = 'low'
    normal = 'normal'
    high = 'high'
    urgent = 'urgent'

class GuestRequest(Base):
    __tablename__ = 'guest_requests'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    booking_id = Column(Integer, ForeignKey('bookings.id'), nullable=False, index=True)
    room_id = Column(Integer, ForeignKey('rooms.id'), nullable=False, index=True)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False)  # Guest who made the request
    
    request_type = Column(Enum(RequestType), nullable=False)
    status = Column(Enum(RequestStatus), nullable=False, default=RequestStatus.pending)
    priority = Column(Enum(RequestPriority), nullable=False, default=RequestPriority.normal)
    
    title = Column(String(255), nullable=False)
    description = Column(Text, nullable=True)
    
    # Assignment
    assigned_to = Column(Integer, ForeignKey('users.id'), nullable=True)  # Housekeeping staff
    fulfilled_by = Column(Integer, ForeignKey('users.id'), nullable=True)
    
    # Timestamps
    requested_at = Column(DateTime, nullable=False, default=datetime.utcnow, index=True)
    started_at = Column(DateTime, nullable=True)
    fulfilled_at = Column(DateTime, nullable=True)
    
    # Notes
    guest_notes = Column(Text, nullable=True)
    staff_notes = Column(Text, nullable=True)
    
    # Response time tracking
    response_time_minutes = Column(Integer, nullable=True)  # Time from request to start
    fulfillment_time_minutes = Column(Integer, nullable=True)  # Time from start to fulfillment
    
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    # Relationships
    booking = relationship('Booking')
    room = relationship('Room')
    guest = relationship('User', foreign_keys=[user_id])
    assigned_staff = relationship('User', foreign_keys=[assigned_to])
    fulfilled_staff = relationship('User', foreign_keys=[fulfilled_by])

