"""Schemas for the /api/v1/admin/users CMS endpoints."""

from __future__ import annotations

from datetime import datetime
from typing import Optional

from pydantic import BaseModel, EmailStr, Field


class AdminUserItem(BaseModel):
    """One row in the admin users list."""

    id: str
    email: str
    name: Optional[str] = None
    role: str
    auth_provider: str
    is_active: bool
    last_login: Optional[datetime] = None
    created_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class AdminUserListResponse(BaseModel):
    """Paginated list of users for the admin CMS."""

    items: list[AdminUserItem]
    total: int
    page: int
    page_size: int


class AdminUpdateRoleRequest(BaseModel):
    """Promote or demote a user between 'admin' and 'user'."""

    role: str = Field(..., pattern="^(admin|user)$")


class AdminUpdateActiveRequest(BaseModel):
    """Enable or disable an account."""

    is_active: bool


class AdminResetPasswordRequest(BaseModel):
    """Set a new password on a local account (admin-initiated)."""

    new_password: str = Field(..., min_length=8, max_length=128)


class ForgotPasswordRequest(BaseModel):
    """Start a password-reset flow for a local account."""

    email: EmailStr


class ResetPasswordConfirmRequest(BaseModel):
    """Complete a password-reset flow using the emailed token."""

    token: str = Field(..., min_length=16, max_length=256)
    new_password: str = Field(..., min_length=8, max_length=128)


class SimpleMessageResponse(BaseModel):
    """Generic success envelope."""

    detail: str