<?php

namespace App\Http\Controllers\Api;

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

class AuditController extends Controller
{
    public function index(Request $request)
    {
        $query = AuditLog::with('user')->latest();

        if ($request->has('user_id')) {
            $query->where('user_id', $request->user_id);
        }

        if ($request->has('module')) {
            $module = $request->module;
            $query->where(function ($q) use ($module) {
                $q->where('module', $module)
                    ->orWhere('module', ' ' . $module);
            });
        }

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

        if ($request->has('date')) {
            $query->whereDate('created_at', $request->date);
        }

        return $query->paginate($request->input('per_page', 20));
    }
}
