<?php

declare(strict_types=1);

use App\Filament\Resources\FindingResource\Pages\ListFindings;
use App\Models\Finding;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

it('supports triage start resolve and reopen via row actions', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'manager');
    $this->actingAs($user);
    Filament::setTenant($tenant, true);

    $finding = Finding::factory()->for($tenant)->create([
        'status' => Finding::STATUS_NEW,
    ]);

    Livewire::test(ListFindings::class)
        ->callTableAction('triage', $finding)
        ->assertHasNoTableActionErrors();

    $finding->refresh();
    expect($finding->status)->toBe(Finding::STATUS_TRIAGED)
        ->and($finding->triaged_at)->not->toBeNull();

    Livewire::test(ListFindings::class)
        ->callTableAction('start_progress', $finding)
        ->assertHasNoTableActionErrors();

    $finding->refresh();
    expect($finding->status)->toBe(Finding::STATUS_IN_PROGRESS)
        ->and($finding->in_progress_at)->not->toBeNull();

    Livewire::test(ListFindings::class)
        ->callTableAction('resolve', $finding, [
            'resolved_reason' => 'patched',
        ])
        ->assertHasNoTableActionErrors();

    $finding->refresh();
    expect($finding->status)->toBe(Finding::STATUS_RESOLVED)
        ->and($finding->resolved_reason)->toBe('patched')
        ->and($finding->resolved_at)->not->toBeNull();

    Livewire::test(ListFindings::class)
        ->filterTable('open', false)
        ->callTableAction('reopen', $finding)
        ->assertHasNoTableActionErrors();

    $finding->refresh();
    expect($finding->status)->toBe(Finding::STATUS_REOPENED)
        ->and($finding->reopened_at)->not->toBeNull()
        ->and($finding->due_at)->not->toBeNull();
});

it('supports close and risk accept via row actions', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);
    Filament::setTenant($tenant, true);

    $closeFinding = Finding::factory()->for($tenant)->create([
        'status' => Finding::STATUS_NEW,
    ]);

    $riskFinding = Finding::factory()->for($tenant)->create([
        'status' => Finding::STATUS_NEW,
    ]);

    Livewire::test(ListFindings::class)
        ->callTableAction('close', $closeFinding, [
            'closed_reason' => 'duplicate ticket',
        ])
        ->assertHasNoTableActionErrors();

    Livewire::test(ListFindings::class)
        ->callTableAction('risk_accept', $riskFinding, [
            'closed_reason' => 'accepted by security',
        ])
        ->assertHasNoTableActionErrors();

    expect($closeFinding->refresh()->status)->toBe(Finding::STATUS_CLOSED)
        ->and($closeFinding->closed_reason)->toBe('duplicate ticket');

    expect($riskFinding->refresh()->status)->toBe(Finding::STATUS_RISK_ACCEPTED)
        ->and($riskFinding->closed_reason)->toBe('accepted by security');
});

it('assigns owners and assignees via row action and rejects non-member ids', function (): void {
    [$manager, $tenant] = createUserWithTenant(role: 'manager');
    $this->actingAs($manager);
    Filament::setTenant($tenant, true);

    $assignee = User::factory()->create();
    createUserWithTenant(tenant: $tenant, user: $assignee, role: 'operator');

    $outsider = User::factory()->create();

    $finding = Finding::factory()->for($tenant)->create([
        'status' => Finding::STATUS_NEW,
    ]);

    Livewire::test(ListFindings::class)
        ->callTableAction('assign', $finding, [
            'assignee_user_id' => (int) $assignee->getKey(),
            'owner_user_id' => (int) $manager->getKey(),
        ])
        ->assertHasNoTableActionErrors();

    $finding->refresh();
    expect((int) $finding->assignee_user_id)->toBe((int) $assignee->getKey())
        ->and((int) $finding->owner_user_id)->toBe((int) $manager->getKey());

    Livewire::test(ListFindings::class)
        ->callTableAction('assign', $finding, [
            'assignee_user_id' => (int) $outsider->getKey(),
            'owner_user_id' => (int) $manager->getKey(),
        ]);

    $finding->refresh();
    expect((int) $finding->assignee_user_id)->toBe((int) $assignee->getKey());
});
