<?php

use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\FieldJob;
use App\Models\FieldJobSetting;
use App\Models\User;
use Inertia\Testing\AssertableInertia as Assert;

it('shows contracts completed today for the user when completed mode is on', function () {
    $user = User::factory()->create();

    // Create a client case and a contract under it
    $case = ClientCase::factory()->create();
    $contract = Contract::factory()->create([
        'client_case_id' => $case->id,
    ]);

    // Create field job setting (required FK)
    $setting = FieldJobSetting::factory()->create();

    // Create a completed field job for this user, for this contract, completed today
    FieldJob::query()->create([
        'field_job_setting_id' => $setting->id,
        'assigned_user_id' => $user->id,
        'user_id' => $user->id,
        'contract_id' => $contract->id,
        'assigned_at' => now()->subHour(),
        'completed_at' => now()->subMinutes(5),
        'cancelled_at' => null,
        'priority' => false,
    ]);

    // In normal mode (no completed flag), the contract should not be listed (job is completed)
    $this->actingAs($user)
        ->get(route('phone.case', ['client_case' => $case->uuid]))
        ->assertInertia(fn (Assert $page) => $page
            ->component('Phone/Case/Index')
            ->where('completed_mode', false)
            ->has('contracts', 0)
        );

    // With completed=1, it should be listed
    $this->actingAs($user)
        ->get(route('phone.case', ['client_case' => $case->uuid, 'completed' => 1]))
        ->assertInertia(fn (Assert $page) => $page
            ->component('Phone/Case/Index')
            ->where('completed_mode', true)
            ->has('contracts', 1)
            ->where('contracts.0.uuid', $contract->uuid)
        );
});
