"""
Pydantic schemas for room-related requests and responses.
"""
from pydantic import BaseModel, Field, field_validator
from typing import Optional, List


class CreateRoomRequest(BaseModel):
    """Schema for creating a room."""
    room_type_id: int = Field(..., gt=0, description="Room type ID")
    room_number: str = Field(..., min_length=1, max_length=20, description="Room number")
    floor: int = Field(..., ge=1, le=200, description="Floor number")
    status: str = Field("available", description="Room status")
    featured: bool = Field(False, description="Whether room is featured")
    price: Optional[float] = Field(None, ge=0, description="Room price (overrides room type base price)")
    description: Optional[str] = Field(None, max_length=2000, description="Room description")
    capacity: Optional[int] = Field(None, ge=1, le=20, description="Room capacity")
    room_size: Optional[str] = Field(None, max_length=50, description="Room size (e.g., '25 sqm')")
    view: Optional[str] = Field(None, max_length=100, description="Room view")
    amenities: Optional[List[str]] = Field(default_factory=list, description="List of amenities")

    @field_validator('status')
    @classmethod
    def validate_status(cls, v: str) -> str:
        """Validate room status."""
        allowed_statuses = ['available', 'occupied', 'maintenance', 'cleaning', 'reserved']
        if v not in allowed_statuses:
            raise ValueError(f'Status must be one of: {", ".join(allowed_statuses)}')
        return v

    @field_validator('amenities')
    @classmethod
    def validate_amenities(cls, v: Optional[List[str]]) -> List[str]:
        """Ensure amenities is a list."""
        if v is None:
            return []
        if not isinstance(v, list):
            return []
        return v

    model_config = {
        "json_schema_extra": {
            "example": {
                "room_type_id": 1,
                "room_number": "101",
                "floor": 1,
                "status": "available",
                "featured": False,
                "price": 150.00,
                "description": "Spacious room with city view",
                "capacity": 2,
                "room_size": "25 sqm",
                "view": "City View",
                "amenities": ["WiFi", "TV", "AC"]
            }
        }
    }


class UpdateRoomRequest(BaseModel):
    """Schema for updating a room."""
    room_type_id: Optional[int] = Field(None, gt=0, description="Room type ID")
    room_number: Optional[str] = Field(None, min_length=1, max_length=20, description="Room number")
    floor: Optional[int] = Field(None, ge=1, le=200, description="Floor number")
    status: Optional[str] = Field(None, description="Room status")
    featured: Optional[bool] = Field(None, description="Whether room is featured")
    price: Optional[float] = Field(None, ge=0, description="Room price")
    description: Optional[str] = Field(None, max_length=2000, description="Room description")
    capacity: Optional[int] = Field(None, ge=1, le=20, description="Room capacity")
    room_size: Optional[str] = Field(None, max_length=50, description="Room size")
    view: Optional[str] = Field(None, max_length=100, description="Room view")
    amenities: Optional[List[str]] = Field(None, description="List of amenities")

    @field_validator('status')
    @classmethod
    def validate_status(cls, v: Optional[str]) -> Optional[str]:
        """Validate room status."""
        if v:
            allowed_statuses = ['available', 'occupied', 'maintenance', 'cleaning', 'reserved']
            if v not in allowed_statuses:
                raise ValueError(f'Status must be one of: {", ".join(allowed_statuses)}')
        return v

    @field_validator('amenities')
    @classmethod
    def validate_amenities(cls, v: Optional[List[str]]) -> Optional[List[str]]:
        """Ensure amenities is a list."""
        if v is not None and not isinstance(v, list):
            return []
        return v


class BulkDeleteRoomsRequest(BaseModel):
    """Schema for bulk deleting rooms."""
    room_ids: List[int] = Field(..., min_items=1, max_items=100, description="List of room IDs to delete")

    @field_validator('room_ids')
    @classmethod
    def validate_room_ids(cls, v: List[int]) -> List[int]:
        """Validate room IDs are positive integers."""
        if not v:
            raise ValueError('At least one room ID is required')
        for room_id in v:
            if room_id <= 0:
                raise ValueError('All room IDs must be positive integers')
        return v


class UpdateAmenityRequest(BaseModel):
    """Schema for updating/renaming an amenity."""
    new_name: str = Field(..., min_length=1, max_length=100, description="New name for the amenity")


class CreateRoomTypeRequest(BaseModel):
    """Schema for creating a room type."""
    name: str = Field(..., min_length=1, max_length=100, description="Room type name")
    description: Optional[str] = Field(None, max_length=2000, description="Room type description")
    base_price: float = Field(..., ge=0, description="Base price for this room type")
    capacity: int = Field(..., ge=1, le=20, description="Room capacity")
    amenities: Optional[List[str]] = Field(default_factory=list, description="List of amenities")

    @field_validator('amenities')
    @classmethod
    def validate_amenities(cls, v: Optional[List[str]]) -> List[str]:
        """Ensure amenities is a list."""
        if v is None:
            return []
        if not isinstance(v, list):
            return []
        return v

    model_config = {
        "json_schema_extra": {
            "example": {
                "name": "Deluxe Suite",
                "description": "Spacious suite with premium amenities",
                "base_price": 250.00,
                "capacity": 2,
                "amenities": ["WiFi", "TV", "AC", "Mini Bar"]
            }
        }
    }


class UpdateRoomTypeRequest(BaseModel):
    """Schema for updating a room type."""
    name: Optional[str] = Field(None, min_length=1, max_length=100, description="Room type name")
    description: Optional[str] = Field(None, max_length=2000, description="Room type description")
    base_price: Optional[float] = Field(None, ge=0, description="Base price for this room type")
    capacity: Optional[int] = Field(None, ge=1, le=20, description="Room capacity")
    amenities: Optional[List[str]] = Field(None, description="List of amenities")

    @field_validator('amenities')
    @classmethod
    def validate_amenities(cls, v: Optional[List[str]]) -> Optional[List[str]]:
        """Ensure amenities is a list."""
        if v is not None and not isinstance(v, list):
            return []
        return v

