<?php

namespace App\Http\Controllers;

use App\Models\AuditLog;
use Illuminate\Http\Request;

class AuditLogController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:audit.view');
    }

    public function index(Request $request)
    {
        $logs = AuditLog::query()
            ->with('user')
            ->search($request->get('q'))
            ->when($request->action, fn ($q, $v) => $q->where('action', $v))
            ->latest()
            ->paginate(per_page())
            ->withQueryString();

        return view('audit.index', compact('logs'));
    }

    public function show(AuditLog $audit)
    {
        $audit->load('user');

        return view('audit.show', ['log' => $audit]);
    }
}
