<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Models\OperationLog;
use App\Support\ApiResponse;
use hg\apidoc\annotation as Apidoc;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

#[Apidoc\Group('后台')]
#[Apidoc\Title('操作日志')]
#[Apidoc\RouteMiddleware(['jwt.auth'])]
final class LogController extends Controller
{
    #[Apidoc\Title('日志列表')]
    #[Apidoc\Url('/admin/logs')]
    #[Apidoc\Method('GET')]
    #[Apidoc\RouteMiddleware(['permission:logs'])]
    public function index(Request $request): JsonResponse
    {
        $query = OperationLog::query()->with('user')->latest();
        if ($action = $request->query('action')) {
            $query->where('action', 'like', '%'.$action.'%');
        }

        return ApiResponse::page($query->paginate((int) $request->query('per_page', 20)));
    }
}
