"""代理：仅查看名下用户（agent_id 匹配）。"""

from fastapi import APIRouter, Query

from app.dependencies import AgentUser, DbSession
from app.schemas.admin_user import AdminUserOut
from app.schemas.common import ApiResponse, PaginatedData
from app.services import agent_user as agent_user_service

router = APIRouter(prefix="/agent/users", tags=["代理-用户"])


@router.get("", response_model=ApiResponse[PaginatedData[AdminUserOut]])
async def list_users(
    agent: AgentUser,
    db: DbSession,
    page: int = Query(1, ge=1, description="页码，从 1 开始"),
    page_size: int = Query(
        agent_user_service.DEFAULT_PAGE_SIZE,
        ge=1,
        le=agent_user_service.MAX_PAGE_SIZE,
        description="每页条数",
    ),
    username: str | None = Query(None, description="用户名模糊搜索"),
    role_id: int | None = Query(None, ge=0, le=3, description="按角色筛选"),
) -> ApiResponse[PaginatedData[AdminUserOut]]:
    users, total = await agent_user_service.list_users(
        db,
        agent_id=agent.id,
        page=page,
        page_size=page_size,
        username=username,
        role_id=role_id,
    )
    return ApiResponse(
        ok=True,
        message="",
        data=PaginatedData.build(
            [AdminUserOut.model_validate(u) for u in users],
            total=total,
            page=page,
            page_size=page_size,
        ),
    )
