"""Auth database 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 AuthUserResponse(BaseModel):
    """Auth user DTO returned from API."""

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

    id: int
    name: Optional[str] = None
    email: str
    is_admin: Optional[bool] = None
    has_write_access: Optional[bool] = None
    is_active: Optional[bool] = None
    role: Optional[str] = None
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None


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

    model_config = ConfigDict(protected_namespaces=())

    email: EmailStr = Field(..., description="Email address (unique, required)")
    name: str = Field(..., min_length=1, max_length=200, description="Name (required)")
    password: str = Field(..., min_length=6, description="Password (minimum 6 characters, required)")
    is_admin: bool = Field(..., description="Admin role (required)")
    has_write_access: bool = Field(..., description="Write access (required)")


class AuthUserUpdateRequest(BaseModel):
    """Request payload to update an auth user."""

    model_config = ConfigDict(protected_namespaces=())

    email: EmailStr = Field(..., description="Email address (required)")
    name: str = Field(..., min_length=1, max_length=200, description="Name (required)")
    is_admin: bool = Field(..., description="Admin role (required)")
    has_write_access: bool = Field(..., description="Write access (required)")
    is_active: Optional[bool] = Field(None, description="Active status (optional)")
    role: Optional[str] = Field(None, description="Role: 'Admin' or 'User' (optional)")
    password: Optional[str] = Field(None, min_length=6, description="New password (optional, minimum 6 characters, leave empty to keep current)")


class AuthUsersListResponse(BaseModel):
    """List of auth users."""

    model_config = ConfigDict(protected_namespaces=())

    items: list[AuthUserResponse]
    total: int


