"""
Room and RoomType models for managing hotel rooms.
"""
import enum
from sqlalchemy import Column, String, Integer, Float, Boolean, Enum, ForeignKey, Text
from sqlalchemy.orm import relationship

from app.models.base import BaseModel


class RoomStatus(str, enum.Enum):
    """Room operational status."""
    AVAILABLE = "AVAILABLE"
    RESERVED = "RESERVED"
    OCCUPIED = "OCCUPIED"
    MAINTENANCE = "MAINTENANCE"
    CLEANING = "CLEANING"
    OUT_OF_ORDER = "OUT_OF_ORDER"


class RoomType(BaseModel):
    """
    Room type configuration.

    Attributes:
        establishment_id: Parent establishment
        name: Room type name (e.g., "Standard", "Suite")
        name_en: Room type name in English
        code: Short code (e.g., "STD", "STE")
        capacity_adults: Max adults capacity
        capacity_children: Max children capacity
        capacity_beds: Number of beds
        base_price: Base price per night
        description: Room type description
        amenities: JSON/text list of amenities
    """
    __tablename__ = "room_types"

    establishment_id = Column(
        Integer,
        ForeignKey("establishments.id", ondelete="CASCADE"),
        nullable=False,
        index=True
    )
    name = Column(String(100), nullable=False)
    name_en = Column(String(100), nullable=True)
    code = Column(String(10), nullable=False)
    capacity_adults = Column(Integer, default=2, nullable=False)
    capacity_children = Column(Integer, default=0, nullable=False)
    capacity_beds = Column(Integer, default=1, nullable=False)
    base_price = Column(Float, default=0.0, nullable=False)
    description = Column(Text, nullable=True)
    description_en = Column(Text, nullable=True)
    amenities = Column(Text, nullable=True)
    is_active = Column(Boolean, default=True, nullable=False)

    # Relationships
    establishment = relationship("Establishment", back_populates="room_types")
    rooms = relationship(
        "Room",
        back_populates="room_type",
        cascade="all, delete-orphan"
    )

    @property
    def total_capacity(self) -> int:
        """Total person capacity."""
        return self.capacity_adults + self.capacity_children

    def __repr__(self) -> str:
        return f"<RoomType {self.code}: {self.name}>"


class Room(BaseModel):
    """
    Individual room in the establishment.

    Attributes:
        establishment_id: Parent establishment
        room_type_id: Room type reference
        room_number: Room number/identifier
        floor: Floor number
        status: Current room status
        notes: Internal notes
    """
    __tablename__ = "rooms"

    establishment_id = Column(
        Integer,
        ForeignKey("establishments.id", ondelete="CASCADE"),
        nullable=False,
        index=True
    )
    room_type_id = Column(
        Integer,
        ForeignKey("room_types.id", ondelete="RESTRICT"),
        nullable=False,
        index=True
    )
    room_number = Column(String(20), nullable=False)
    floor = Column(Integer, default=0, nullable=False)
    status = Column(
        Enum(RoomStatus),
        default=RoomStatus.AVAILABLE,
        nullable=False,
        index=True
    )
    notes = Column(Text, nullable=True)
    is_active = Column(Boolean, default=True, nullable=False)

    # Relationships
    establishment = relationship("Establishment", back_populates="rooms")
    room_type = relationship("RoomType", back_populates="rooms")
    reservations = relationship("Reservation", back_populates="room")

    def __repr__(self) -> str:
        return f"<Room {self.room_number}>"
