<?php

declare(strict_types=1);

use App\Filament\Resources\FindingResource;
use App\Models\BaselineProfile;
use App\Models\BaselineSnapshot;
use App\Models\Finding;
use App\Models\OperationRun;
use App\Models\Policy;
use App\Models\PolicyVersion;
use Filament\Facades\Filament;

it('renders finding related context with human labels before technical ids', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);
    Filament::setTenant($tenant, true);

    $profile = BaselineProfile::factory()->active()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'name' => 'Security Baseline',
    ]);

    $snapshot = BaselineSnapshot::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'baseline_profile_id' => (int) $profile->getKey(),
    ]);

    $policy = Policy::factory()->for($tenant)->create([
        'display_name' => 'Windows Lockdown',
    ]);

    $version = PolicyVersion::factory()->for($tenant)->create([
        'policy_id' => (int) $policy->getKey(),
        'version_number' => 3,
    ]);

    $run = OperationRun::factory()->for($tenant)->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'type' => 'baseline_compare',
    ]);

    $finding = Finding::factory()->for($tenant)->create([
        'current_operation_run_id' => (int) $run->getKey(),
        'evidence_jsonb' => [
            'current' => [
                'policy_version_id' => (int) $version->getKey(),
            ],
            'provenance' => [
                'baseline_profile_id' => (int) $profile->getKey(),
                'baseline_snapshot_id' => (int) $snapshot->getKey(),
                'compare_operation_run_id' => (int) $run->getKey(),
            ],
        ],
    ]);

    $this->get(FindingResource::getUrl('view', ['record' => $finding], tenant: $tenant))
        ->assertOk()
        ->assertSee('Security Baseline')
        ->assertSee('Baseline snapshot #'.$snapshot->getKey())
        ->assertSee('Windows Lockdown')
        ->assertSee('Version 3')
        ->assertSee('Baseline compare')
        ->assertSee('Operation #'.$run->getKey());
});

it('shows canonical manual terminal outcome and verification labels on finding detail', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);
    Filament::setTenant($tenant, true);

    $finding = Finding::factory()->for($tenant)->create([
        'status' => Finding::STATUS_RESOLVED,
        'resolved_reason' => Finding::RESOLVE_REASON_REMEDIATED,
        'resolved_at' => now()->subHour(),
    ]);

    $this->get(FindingResource::getUrl('view', ['record' => $finding], tenant: $tenant))
        ->assertOk()
        ->assertSee('Terminal outcome')
        ->assertSee('Resolved pending verification')
        ->assertSee('Verification')
        ->assertSee('Pending verification')
        ->assertSee('Resolved reason')
        ->assertSee('Remediated');
});

it('shows verified clear and administrative closure labels on finding detail', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);
    Filament::setTenant($tenant, true);

    $verifiedFinding = Finding::factory()->for($tenant)->create([
        'status' => Finding::STATUS_RESOLVED,
        'resolved_reason' => Finding::RESOLVE_REASON_NO_LONGER_DRIFTING,
        'resolved_at' => now()->subHour(),
    ]);

    $closedFinding = Finding::factory()->for($tenant)->create([
        'status' => Finding::STATUS_CLOSED,
        'closed_reason' => Finding::CLOSE_REASON_DUPLICATE,
        'closed_at' => now()->subHour(),
    ]);

    $this->get(FindingResource::getUrl('view', ['record' => $verifiedFinding], tenant: $tenant))
        ->assertOk()
        ->assertSee('Verified cleared')
        ->assertSee('No longer drifting');

    $this->get(FindingResource::getUrl('view', ['record' => $closedFinding], tenant: $tenant))
        ->assertOk()
        ->assertSee('Closed as duplicate')
        ->assertSee('Duplicate');
});
