<?php

namespace App\Http\Controllers\API\SuperAdmin;

use App\Http\Controllers\API\BaseApiController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use OwenIt\Auditing\Models\Audit;

class AuditLogController extends BaseApiController
{
    public function index(Request $request): JsonResponse
    {
        $audits = Audit::with('user:id,uid,name,email')
            ->when($request->user_id, fn($q) => $q->where('user_id', $request->user_id))
            ->when($request->event, fn($q) => $q->where('event', $request->event))
            ->when($request->auditable_type, fn($q) => $q->where('auditable_type', 'like', "%{$request->auditable_type}%"))
            ->when($request->from, fn($q) => $q->whereDate('created_at', '>=', $request->from))
            ->when($request->to, fn($q) => $q->whereDate('created_at', '<=', $request->to))
            ->orderByDesc('created_at')
            ->paginate(25);

        return $this->paginatedResponse($audits);
    }

    public function show(int $id): JsonResponse
    {
        $audit = Audit::with('user:id,uid,name,email')->findOrFail($id);
        return $this->successResponse($audit);
    }
}
