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


class CreateUserRequest(BaseModel):
    """Schema for creating a user."""
    full_name: str = Field(..., min_length=2, max_length=100, description="Full name")
    email: EmailStr = Field(..., description="Email address")
    password: str = Field(..., min_length=8, description="Password")
    phone_number: Optional[str] = Field(None, max_length=20, description="Phone number")
    role_id: Optional[int] = Field(None, gt=0, description="Role ID")
    role: Optional[str] = Field(None, description="Role name (alternative to role_id)")

    @field_validator('password')
    @classmethod
    def validate_password(cls, v: str) -> str:
        """Validate password strength."""
        if len(v) < 8:
            raise ValueError('Password must be at least 8 characters')
        if not any(c.isupper() for c in v):
            raise ValueError('Password must contain at least one uppercase letter')
        if not any(c.islower() for c in v):
            raise ValueError('Password must contain at least one lowercase letter')
        if not any(c.isdigit() for c in v):
            raise ValueError('Password must contain at least one number')
        return v


class UpdateUserRequest(BaseModel):
    """Schema for updating a user."""
    full_name: Optional[str] = Field(None, min_length=2, max_length=100)
    email: Optional[EmailStr] = None
    phone_number: Optional[str] = Field(None, max_length=20)
    role_id: Optional[int] = Field(None, gt=0)
    role: Optional[str] = Field(None, description="Role name (alternative to role_id)")
    is_active: Optional[bool] = None

