"""admin 账号管理 + 审计日志 schemas。"""
from __future__ import annotations

from datetime import datetime
from typing import Literal

from pydantic import BaseModel, ConfigDict, Field

# 角色不再硬编码枚举:改为任意角色名(自定义角色由 admin_role 表管理),存在性在路由层校验。


class AdminCreateRequest(BaseModel):
    username: str = Field(..., min_length=3, max_length=64)
    password: str = Field(..., min_length=8, max_length=72)  # bcrypt ≤72 字节
    role: str = Field("operator", min_length=1, max_length=32)
    # 仅当 role == "custom":这个人专属可见页 key 列表(逐页勾选结果)。其余角色不传/忽略。
    pages_override: list[str] | None = None


class AdminUpdateRequest(BaseModel):
    """改角色 / 启用禁用 / 重置密码 / 自定义可见页,字段都可选(只改传了的)。"""

    role: str | None = Field(None, min_length=1, max_length=32)
    status: Literal["active", "disabled"] | None = None
    password: str | None = Field(None, min_length=8, max_length=72)
    # 改成/更新「自定义」可见页;role 切回普通角色时后端会清空 override(见路由)。
    pages_override: list[str] | None = None


class AdminAuditLogOut(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: int
    admin_id: int
    admin_username: str
    action: str
    target_type: str
    target_id: str | None = None
    detail: dict | None = None
    ip: str | None = None
    created_at: datetime
