<?php

namespace Tests\Feature;

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

class NotificationsUnreadFilterTest extends TestCase
{
    use RefreshDatabase;

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

        // Create two client cases (each with a contract)
        $caseA = ClientCase::factory()->create();
        $contractA = Contract::factory()->create(['client_case_id' => $caseA->id]);

        $caseB = ClientCase::factory()->create();
        $contractB = Contract::factory()->create(['client_case_id' => $caseB->id]);

        // Create required related models for activities
        $action = \App\Models\Action::factory()->create();
        $decision = \App\Models\Decision::factory()->create();

        // Create activities due today: one tied to contract (A), one tied directly to client case (B)
        Activity::query()->create([
            'due_date' => now()->toDateString(),
            'amount' => 100,
            'action_id' => $action->id,
            'decision_id' => $decision->id,
            'client_case_id' => $caseA->id,
            'contract_id' => $contractA->id,
        ]);

        Activity::query()->create([
            'due_date' => now()->toDateString(),
            'amount' => 200,
            'action_id' => $action->id,
            'decision_id' => $decision->id,
            'client_case_id' => $caseB->id,
        ]);

        // Request unread without filter
        $resp = $this->get(route('notifications.unread'));
        $resp->assertStatus(200);

        $resp->assertInertia(function (Assert $page) use ($caseA, $caseB) {
            $props = $page->toArray()['props'];
            // Activities: both should be present
            $this->assertEquals(2, (int) ($props['activities']['total'] ?? 0));

            // Clients list should include both case UUIDs
            $clientValues = collect($props['clients'] ?? [])->pluck('value')->all();
            $this->assertContains($caseA->uuid, $clientValues);
            $this->assertContains($caseB->uuid, $clientValues);
        });

        // Apply filter for case A
        $resp2 = $this->get(route('notifications.unread', ['client' => $caseA->uuid]));
        $resp2->assertStatus(200);

        $resp2->assertInertia(function (Assert $page) use ($caseA) {
            $props = $page->toArray()['props'];
            // Only one activity should be returned for case A
            $this->assertEquals(1, (int) ($props['activities']['total'] ?? 0));
            $data = $props['activities']['data'] ?? [];
            $this->assertCount(1, $data);

            // Assert the activity relates to the requested client case (either directly or via contract)
            $row = $data[0] ?? [];
            $contractCaseUuid = data_get($row, 'contract.client_case.uuid');
            $directCaseUuid = data_get($row, 'client_case.uuid');
            $this->assertTrue($contractCaseUuid === $caseA->uuid || $directCaseUuid === $caseA->uuid);
        });
    }
}
