"""admin 操作审计日志查询(所有 admin 可看:谁在何时对什么做了什么)。"""
from __future__ import annotations

from typing import Annotated

from fastapi import APIRouter, Depends, Query

from app.admin.deps import AdminDb, get_current_admin
from app.admin.repositories import audit_log as audit_repo
from app.admin.schemas.admin import AdminAuditLogOut
from app.admin.schemas.common import CursorPage

router = APIRouter(
    prefix="/admin/api/audit-logs",
    tags=["admin-audit"],
    dependencies=[Depends(get_current_admin)],
)


@router.get("", response_model=CursorPage[AdminAuditLogOut], summary="审计日志(谁改了什么)")
def list_audit_logs(
    db: AdminDb,
    action: Annotated[str | None, Query()] = None,
    target_type: Annotated[str | None, Query()] = None,
    admin_id: Annotated[int | None, Query()] = None,
    limit: Annotated[int, Query(ge=1, le=100)] = 50,
    cursor: Annotated[int | None, Query()] = None,
) -> CursorPage[AdminAuditLogOut]:
    items, next_cursor, total = audit_repo.list_audit_logs(
        db, action=action, target_type=target_type, admin_id=admin_id, limit=limit, cursor=cursor,
    )
    return CursorPage(
        items=[AdminAuditLogOut.model_validate(x) for x in items],
        next_cursor=next_cursor,
        total=total,
    )
