"""User management schemas for web API."""

from __future__ import annotations

from datetime import datetime
from typing import Optional

from pydantic import BaseModel, ConfigDict, EmailStr, Field


class UserResponse(BaseModel):
    """User DTO returned from API."""

    model_config = ConfigDict(from_attributes=True, protected_namespaces=())

    id: int
    username: str
    email: Optional[str] = None
    full_name: Optional[str] = None
    is_active: bool
    is_admin: bool
    password_change_required: bool
    created_date: datetime
    last_login: Optional[datetime] = None


class UserCreateRequest(BaseModel):
    """Request payload to create a new user."""

    model_config = ConfigDict(protected_namespaces=())

    username: str = Field(..., min_length=1, max_length=100)
    password: str = Field(..., min_length=6, description="Password (minimum 6 characters)")
    email: Optional[EmailStr] = None
    full_name: Optional[str] = Field(None, max_length=200)
    is_active: bool = True
    is_admin: bool = False


class UserUpdateRequest(BaseModel):
    """Request payload to update a user."""

    model_config = ConfigDict(protected_namespaces=())

    password: Optional[str] = Field(None, min_length=6, description="New password (minimum 6 characters, leave empty to keep current)")
    email: Optional[EmailStr] = None
    full_name: Optional[str] = Field(None, max_length=200)
    is_active: Optional[bool] = None
    is_admin: Optional[bool] = None


class UsersListResponse(BaseModel):
    """List of users."""

    model_config = ConfigDict(protected_namespaces=())

    items: list[UserResponse]
    total: int

