<?php

declare(strict_types=1);

use App\Filament\System\Pages\Ops\Controls;
use App\Models\PlatformUser;
use App\Support\Auth\PlatformCapabilities;
use App\Support\OperationalControls\OperationalControlCatalog;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    Filament::setCurrentPanel('system');
    Filament::bootCurrentPanel();
});

it('does not expose findings lifecycle backfill in operational controls or the control catalog', function (): void {
    $user = PlatformUser::factory()->create([
        'capabilities' => [
            PlatformCapabilities::ACCESS_SYSTEM_PANEL,
            PlatformCapabilities::OPS_CONTROLS_MANAGE,
        ],
        'is_active' => true,
    ]);

    $this->actingAs($user, 'platform');

    $this->get(Controls::getUrl(panel: 'system'))
        ->assertSuccessful()
        ->assertDontSee('Findings lifecycle backfill')
        ->assertDontSee("mountAction('pause_findings_lifecycle_backfill')", escape: false)
        ->assertDontSee("mountAction('resume_findings_lifecycle_backfill')", escape: false)
        ->assertDontSee("mountAction('view_history_findings_lifecycle_backfill')", escape: false);

    $catalog = app(OperationalControlCatalog::class);

    expect($catalog->keys())->not->toContain('findings.lifecycle.backfill')
        ->and(fn (): array => $catalog->definition('findings.lifecycle.backfill'))
        ->toThrow(InvalidArgumentException::class);
});
