<?php

use App\Models\Client;
use App\Models\ClientCase;
use App\Models\Contract;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('supports contracts_page and contracts_per_page parameters', function () {
    // Create a client with a single client case and multiple contracts
    $client = Client::factory()->create();
    $case = ClientCase::factory()->create(['client_id' => $client->id]);

    // Create 5 contracts tied to the case with deterministic references
    foreach (range(1, 5) as $i) {
        Contract::factory()->create([
            'client_case_id' => $case->id,
            'reference' => 'REF-'.$i,
        ]);
    }

    // Request page 2 with page size 2 using custom param names
    $response = $this->get(route('client.contracts', ['client' => $client->uuid, 'contracts_page' => 2, 'contracts_per_page' => 2]));
    $response->assertStatus(200);

    // Extract props
    $props = $response->inertia()->toArray()['props'];
    expect($props['contracts']['per_page'])->toBe(2)
        ->and($props['contracts']['current_page'])->toBe(2)
        ->and($props['contracts']['data'])->toHaveCount(2);

    // Ensure links use the custom page parameter name
    $anyLinkWithCustomParam = collect($props['contracts']['links'])->contains(function ($link) {
        return str_contains($link['url'] ?? '', 'contracts_page=');
    });
    expect($anyLinkWithCustomParam)->toBeTrue();
});
