<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AuditLog;
use App\Services\BranchAccessService;
use Illuminate\Http\Request;
use Illuminate\View\View;

class AuditLogController extends Controller
{
    public function index(Request $request, BranchAccessService $branchAccess): View
    {
        $query = AuditLog::with(['user', 'branch'])->latest();
        if (! $branchAccess->isSystemWide($request->user())) {
            $query->whereIn('branch_id', $request->user()->branches()->select('branches.id'));
        }
        if ($request->filled('action')) {
            $query->where('action', 'like', '%'.$request->string('action').'%');
        }

        return view('admin.audit.index', ['logs' => $query->paginate(50)->withQueryString()]);
    }
}
