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

class MaintenanceType(str, enum.Enum):
    preventive = 'preventive'
    corrective = 'corrective'
    emergency = 'emergency'
    upgrade = 'upgrade'
    inspection = 'inspection'

class MaintenanceStatus(str, enum.Enum):
    scheduled = 'scheduled'
    in_progress = 'in_progress'
    completed = 'completed'
    cancelled = 'cancelled'
    on_hold = 'on_hold'

class RoomMaintenance(Base):
    __tablename__ = 'room_maintenance'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    room_id = Column(Integer, ForeignKey('rooms.id'), nullable=False, index=True)
    maintenance_type = Column(Enum(MaintenanceType), nullable=False)
    status = Column(Enum(MaintenanceStatus), nullable=False, default=MaintenanceStatus.scheduled)
    title = Column(String(255), nullable=False)
    description = Column(Text, nullable=True)
    
    # Scheduling
    scheduled_start = Column(DateTime, nullable=False)
    scheduled_end = Column(DateTime, nullable=True)
    actual_start = Column(DateTime, nullable=True)
    actual_end = Column(DateTime, nullable=True)
    
    # Assignment
    assigned_to = Column(Integer, ForeignKey('users.id'), nullable=True)
    reported_by = Column(Integer, ForeignKey('users.id'), nullable=True)
    
    # Cost tracking
    estimated_cost = Column(Numeric(10, 2), nullable=True)
    actual_cost = Column(Numeric(10, 2), nullable=True)
    
    # Room blocking
    blocks_room = Column(Boolean, nullable=False, default=True)
    block_start = Column(DateTime, nullable=True)
    block_end = Column(DateTime, nullable=True)
    
    # Additional info
    priority = Column(String(20), nullable=False, default='medium')  # low, medium, high, urgent
    notes = Column(Text, nullable=True)
    completion_notes = Column(Text, 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='maintenance_records')
    assigned_staff = relationship('User', foreign_keys=[assigned_to])
    reporter = relationship('User', foreign_keys=[reported_by])

