<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AdminAuditLog;
use App\Models\User;
use Illuminate\Http\Request;

class AdminAuditLogController extends Controller
{
    public function index(Request $request)
    {
        $query = AdminAuditLog::with('admin')->latest();

        if ($request->filled('admin_id')) {
            $query->where('admin_id', $request->admin_id);
        }

        if ($request->filled('action')) {
            $query->where('action', $request->action);
        }

        if ($request->filled('target_type')) {
            $query->where('target_type', $request->target_type);
        }

        $logs = $query->paginate(50)->withQueryString();

        $admins = User::where('is_admin', true)->orderBy('name')->get();

        $actions = AdminAuditLog::distinct()->orderBy('action')->pluck('action');

        return view('admin.audit-log.index', compact('logs', 'admins', 'actions'));
    }
}
