from __future__ import annotations

from typing import Annotated

from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session

from app.api.deps import get_db, require_platform_admin_user
from app.schemas.audit_log import AuditLogRead
from app.services.audit import AuditLogService

router = APIRouter(prefix="/audit-logs", dependencies=[Depends(require_platform_admin_user)])
DbSession = Annotated[Session, Depends(get_db)]


@router.get(
    "",
    response_model=list[AuditLogRead],
    summary="查询审计日志",
    description="按资源类型、资源 ID、动作类型和数量限制筛选审计日志。",
)
def list_audit_logs(
    db: DbSession,
    resource_type: Annotated[
        str | None,
        Query(description="资源类型筛选，例如 `rule`、`task`。"),
    ] = None,
    resource_id: Annotated[
        str | None,
        Query(description="资源主键或业务编码筛选。"),
    ] = None,
    action: Annotated[
        str | None,
        Query(description="动作名称筛选。"),
    ] = None,
    limit: Annotated[
        int,
        Query(ge=1, le=200, description="返回日志上限。"),
    ] = 50,
) -> list[AuditLogRead]:
    return AuditLogService(db).list_logs(
        resource_type=resource_type,
        resource_id=resource_id,
        action=action,
        limit=limit,
    )
