"""
Pydantic schemas for guest complaint management.
"""
from pydantic import BaseModel, Field, field_validator
from typing import Optional, List
from datetime import datetime


class CreateComplaintRequest(BaseModel):
    """Schema for creating a guest complaint."""
    booking_id: Optional[int] = Field(None, gt=0, description="Related booking ID")
    room_id: Optional[int] = Field(None, gt=0, description="Related room ID")
    category: str = Field(..., description="Complaint category")
    priority: str = Field("medium", description="Complaint priority")
    title: str = Field(..., min_length=1, max_length=255, description="Complaint title")
    description: str = Field(..., min_length=1, description="Complaint description")
    attachments: Optional[List[str]] = Field(default_factory=list, description="Attachment URLs")
    
    @field_validator('category')
    @classmethod
    def validate_category(cls, v: str) -> str:
        """Validate complaint category."""
        allowed = ['room_quality', 'service', 'cleanliness', 'noise', 'billing', 'staff_behavior', 'amenities', 'other']
        if v not in allowed:
            raise ValueError(f'Category must be one of: {", ".join(allowed)}')
        return v
    
    @field_validator('priority')
    @classmethod
    def validate_priority(cls, v: str) -> str:
        """Validate complaint priority."""
        allowed = ['low', 'medium', 'high', 'urgent']
        if v not in allowed:
            raise ValueError(f'Priority must be one of: {", ".join(allowed)}')
        return v


class UpdateComplaintRequest(BaseModel):
    """Schema for updating a complaint."""
    status: Optional[str] = Field(None, description="New status")
    priority: Optional[str] = Field(None, description="New priority")
    assigned_to: Optional[int] = Field(None, gt=0, description="Assign to user ID")
    resolution: Optional[str] = Field(None, description="Resolution details")
    internal_notes: Optional[str] = Field(None, description="Internal notes")
    requires_follow_up: Optional[bool] = Field(None, description="Requires follow-up")
    follow_up_date: Optional[str] = Field(None, description="Follow-up date (ISO format)")
    
    @field_validator('status')
    @classmethod
    def validate_status(cls, v: Optional[str]) -> Optional[str]:
        """Validate complaint status."""
        if v:
            allowed = ['open', 'in_progress', 'resolved', 'closed', 'escalated']
            if v not in allowed:
                raise ValueError(f'Status must be one of: {", ".join(allowed)}')
        return v
    
    @field_validator('priority')
    @classmethod
    def validate_priority(cls, v: Optional[str]) -> Optional[str]:
        """Validate complaint priority."""
        if v:
            allowed = ['low', 'medium', 'high', 'urgent']
            if v not in allowed:
                raise ValueError(f'Priority must be one of: {", ".join(allowed)}')
        return v


class AddComplaintUpdateRequest(BaseModel):
    """Schema for adding an update to a complaint."""
    update_type: str = Field(..., description="Type of update")
    description: str = Field(..., min_length=1, description="Update description")
    metadata: Optional[dict] = Field(default_factory=dict, description="Additional metadata")
    
    @field_validator('update_type')
    @classmethod
    def validate_update_type(cls, v: str) -> str:
        """Validate update type."""
        allowed = ['status_change', 'assignment', 'note', 'resolution', 'escalation']
        if v not in allowed:
            raise ValueError(f'Update type must be one of: {", ".join(allowed)}')
        return v


class ResolveComplaintRequest(BaseModel):
    """Schema for resolving a complaint."""
    resolution: str = Field(..., min_length=1, description="Resolution details")
    guest_satisfaction_rating: Optional[int] = Field(None, ge=1, le=5, description="Guest satisfaction rating (1-5)")
    guest_feedback: Optional[str] = Field(None, description="Guest feedback on resolution")
    
    @field_validator('guest_satisfaction_rating')
    @classmethod
    def validate_rating(cls, v: Optional[int]) -> Optional[int]:
        """Validate rating."""
        if v is not None and (v < 1 or v > 5):
            raise ValueError('Rating must be between 1 and 5')
        return v

