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

class HousekeepingStatus(str, enum.Enum):
    pending = 'pending'
    in_progress = 'in_progress'
    completed = 'completed'
    skipped = 'skipped'
    cancelled = 'cancelled'

class HousekeepingType(str, enum.Enum):
    checkout = 'checkout'  # Deep cleaning after checkout
    stayover = 'stayover'  # Daily cleaning for occupied rooms
    vacant = 'vacant'  # Cleaning for vacant rooms
    inspection = 'inspection'  # Pre-check-in inspection
    turndown = 'turndown'  # Evening turndown service

class HousekeepingTask(Base):
    __tablename__ = 'housekeeping_tasks'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    room_id = Column(Integer, ForeignKey('rooms.id'), nullable=False, index=True)
    booking_id = Column(Integer, ForeignKey('bookings.id'), nullable=True, index=True)
    
    task_type = Column(Enum(HousekeepingType), nullable=False)
    status = Column(Enum(HousekeepingStatus), nullable=False, default=HousekeepingStatus.pending)
    
    # Scheduling
    scheduled_time = Column(DateTime, nullable=False, index=True)
    started_at = Column(DateTime, nullable=True)
    completed_at = Column(DateTime, nullable=True)
    
    # Assignment
    assigned_to = Column(Integer, ForeignKey('users.id'), nullable=True)
    created_by = Column(Integer, ForeignKey('users.id'), nullable=True)
    
    # Task details
    checklist_items = Column(JSON, nullable=True)  # Array of {item: string, completed: bool, notes: string}
    notes = Column(Text, nullable=True)
    issues_found = Column(Text, nullable=True)
    photos = Column(JSON, nullable=True)  # Array of photo URLs: ["/uploads/housekeeping/photo1.jpg", ...]
    
    # Quality control
    inspected_by = Column(Integer, ForeignKey('users.id'), nullable=True)
    inspected_at = Column(DateTime, nullable=True)
    inspection_notes = Column(Text, nullable=True)
    quality_score = Column(Integer, nullable=True)  # 1-5 rating
    
    # Duration tracking
    estimated_duration_minutes = Column(Integer, nullable=True)
    actual_duration_minutes = Column(Integer, nullable=True)
    
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    # Relationships
    room = relationship('Room', back_populates='housekeeping_tasks')
    booking = relationship('Booking')
    assigned_staff = relationship('User', foreign_keys=[assigned_to])
    creator = relationship('User', foreign_keys=[created_by])
    inspector = relationship('User', foreign_keys=[inspected_by])

