<?php

namespace Tests\Feature;

use App\Models\Client;
use App\Models\ClientCase;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;

class ClientCaseIndexFilterTest extends TestCase
{
    use RefreshDatabase;

    public function test_filters_cases_by_client_and_date(): void
    {
        $user = User::factory()->create();
        $this->actingAs($user);

        $clientInclude = Client::factory()->create(['active' => true]);
        $clientExclude = Client::factory()->create(['active' => true]);

        $recentCase = ClientCase::factory()->create([
            'client_id' => $clientInclude->id,
        ]);
        $recentCase->forceFill([
            'created_at' => now()->subDays(2),
            'updated_at' => now()->subDays(2),
        ])->save();

        $otherClientCase = ClientCase::factory()->create([
            'client_id' => $clientExclude->id,
        ]);
        $otherClientCase->forceFill([
            'created_at' => now()->subDays(2),
            'updated_at' => now()->subDays(2),
        ])->save();

        $outOfRangeCase = ClientCase::factory()->create([
            'client_id' => $clientInclude->id,
        ]);
        $outOfRangeCase->forceFill([
            'created_at' => now()->subDays(15),
            'updated_at' => now()->subDays(15),
        ])->save();

        $response = $this->get(route('clientCase', [
            'clients' => (string) $clientInclude->id,
            'from' => now()->subDays(5)->toDateString(),
            'to' => now()->toDateString(),
        ]));

        $response->assertInertia(function (Assert $page) use ($recentCase, $otherClientCase, $outOfRangeCase, $clientInclude) {
            $page->component('Cases/Index');

            $payload = $page->toArray()['props'];
            $uuids = collect($payload['client_cases']['data'])->pluck('uuid')->all();

            $this->assertContains($recentCase->uuid, $uuids);
            $this->assertNotContains($otherClientCase->uuid, $uuids);
            $this->assertNotContains($outOfRangeCase->uuid, $uuids);
            $this->assertEquals([(string) $clientInclude->id], $payload['filters']['clients']);
        });
    }

    public function test_filters_cases_by_search_term(): void
    {
        $user = User::factory()->create();
        $this->actingAs($user);

        $targetCase = ClientCase::factory()->create();
        $targetCase->person()->update(['full_name' => 'Special Target']);

        $otherCase = ClientCase::factory()->create();
        $otherCase->person()->update(['full_name' => 'Another Person']);

        $response = $this->get(route('clientCase', ['search' => 'Target']));

        $response->assertInertia(function (Assert $page) use ($targetCase, $otherCase) {
            $page->component('Cases/Index');

            $uuids = collect($page->toArray()['props']['client_cases']['data'])->pluck('uuid')->all();

            $this->assertContains($targetCase->uuid, $uuids);
            $this->assertNotContains($otherCase->uuid, $uuids);
        });
    }

    public function test_respects_custom_per_page_selection(): void
    {
        $user = User::factory()->create();
        $this->actingAs($user);

        ClientCase::factory()->count(30)->create();

        $response = $this->get(route('clientCase', ['perPage' => 10]));

        $response->assertInertia(function (Assert $page) {
            $page->component('Cases/Index');

            $payload = $page->toArray()['props'];

            $this->assertCount(10, $payload['client_cases']['data']);
            $this->assertSame(10, $payload['client_cases']['per_page']);
            $this->assertSame(10, $payload['filters']['perPage']);
        });
    }
}
