<?php

namespace App\Http\Controllers\API\SuperAdmin;

use App\Http\Controllers\API\BaseApiController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Spatie\Activitylog\Models\Activity;

class ActivityLogController extends BaseApiController
{
    public function index(Request $request): JsonResponse
    {
        $logs = Activity::with('causer:id,uid,name,email')
            ->when($request->causer_id, fn($q) => $q->where('causer_id', $request->causer_id))
            ->when($request->log_name, fn($q) => $q->where('log_name', $request->log_name))
            ->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($logs);
    }
}
