<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Consent;
use Illuminate\Http\Request;
class ConsentController extends Controller
{
    public function index(Request $request)
    {
        $request->validate([
            'case_id' => ['nullable','integer','exists:cases,id'],
            'page' => ['nullable','integer','min:1'],
            'per_page' => ['nullable','integer','min:1','max:100'],
        ]);
        $perPage = $request->integer('per_page', 15);
        $consents = Consent::when($request->filled('case_id'), fn($q) => $q->where('case_id', $request->case_id))
            ->latest()
            ->paginate($perPage);
        return response()->json([
            'success' => true,
            'message' => 'Consentimentos carregados com sucesso',
            'data' => $consents->items(),
            'meta' => [
                'current_page' => $consents->currentPage(),
                'per_page' => $consents->perPage(),
                'total' => $consents->total(),
                'last_page' => $consents->lastPage(),
            ],
        ]);
    }
}
