"""
Client schemas for guest management.
"""
from typing import Optional, List
from datetime import datetime, date
from pydantic import BaseModel, EmailStr, Field


class ClientBase(BaseModel):
    """Base client schema."""
    first_name: str = Field(..., min_length=1, max_length=100)
    last_name: str = Field(..., min_length=1, max_length=100)
    email: Optional[EmailStr] = None
    phone: Optional[str] = Field(None, max_length=50)
    mobile: Optional[str] = Field(None, max_length=50)
    address: Optional[str] = None
    city: Optional[str] = Field(None, max_length=100)
    postal_code: Optional[str] = Field(None, max_length=20)
    country: Optional[str] = Field(None, max_length=100)
    nationality: Optional[str] = Field(None, max_length=100)
    id_type: Optional[str] = Field(None, max_length=50)
    id_number: Optional[str] = Field(None, max_length=100)
    id_expiry: Optional[date] = None
    date_of_birth: Optional[date] = None
    company_name: Optional[str] = Field(None, max_length=200)
    tax_id: Optional[str] = Field(None, max_length=100)
    notes: Optional[str] = None
    vip: bool = False


class ClientCreate(ClientBase):
    """Schema for creating a client."""
    pass


class ClientUpdate(BaseModel):
    """Schema for updating a client."""
    first_name: Optional[str] = Field(None, min_length=1, max_length=100)
    last_name: Optional[str] = Field(None, min_length=1, max_length=100)
    email: Optional[EmailStr] = None
    phone: Optional[str] = Field(None, max_length=50)
    mobile: Optional[str] = Field(None, max_length=50)
    address: Optional[str] = None
    city: Optional[str] = Field(None, max_length=100)
    postal_code: Optional[str] = Field(None, max_length=20)
    country: Optional[str] = Field(None, max_length=100)
    nationality: Optional[str] = Field(None, max_length=100)
    id_type: Optional[str] = Field(None, max_length=50)
    id_number: Optional[str] = Field(None, max_length=100)
    id_expiry: Optional[date] = None
    date_of_birth: Optional[date] = None
    company_name: Optional[str] = Field(None, max_length=200)
    tax_id: Optional[str] = Field(None, max_length=100)
    notes: Optional[str] = None
    vip: Optional[bool] = None
    blacklisted: Optional[bool] = None
    is_active: Optional[bool] = None


class ClientResponse(ClientBase):
    """Schema for client response."""
    id: int
    full_name: str
    display_name: str
    blacklisted: bool
    is_active: bool
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class ClientSearch(BaseModel):
    """Schema for client search query."""
    query: Optional[str] = Field(None, min_length=2)
    email: Optional[EmailStr] = None
    phone: Optional[str] = None
    id_number: Optional[str] = None
    vip_only: bool = False
    limit: int = Field(20, ge=1, le=100)
    offset: int = Field(0, ge=0)


class ClientHistory(BaseModel):
    """Schema for client stay history."""
    client_id: int
    total_stays: int
    total_nights: int
    total_spent: float
    last_stay: Optional[date] = None
    reservations: List[dict] = []


class ClientListResponse(BaseModel):
    """Paginated client list response."""
    items: List[ClientResponse]
    total: int
    page: int
    page_size: int
    total_pages: int
