<?php

declare(strict_types=1);

use App\Filament\Resources\ManagedEnvironmentResource\Pages\EditManagedEnvironment;
use App\Models\ManagedEnvironment;
use Filament\Actions\Action;
use Filament\Actions\ActionGroup;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;

uses(RefreshDatabase::class);

function editTenantHeaderActions(Testable $component): array
{
    $instance = $component->instance();

    if ($instance->getCachedHeaderActions() === []) {
        $instance->cacheInteractsWithHeaderActions();
    }

    return $instance->getCachedHeaderActions();
}

function editTenantHeaderGroupLabels(Testable $component): array
{
    return collect(editTenantHeaderActions($component))
        ->filter(static fn ($action): bool => $action instanceof ActionGroup && $action->isVisible())
        ->map(static fn (ActionGroup $action): string => (string) $action->getLabel())
        ->values()
        ->all();
}

function editTenantHeaderPrimaryNames(Testable $component): array
{
    return collect(editTenantHeaderActions($component))
        ->reject(static fn ($action): bool => $action instanceof ActionGroup)
        ->filter(static fn ($action): bool => ! method_exists($action, 'isVisible') || $action->isVisible())
        ->map(static fn ($action): ?string => $action instanceof Action ? $action->getName() : null)
        ->filter()
        ->values()
        ->all();
}

it('keeps related links in contextual placement and reserves the header for lifecycle actions', function (): void {
    $tenant = ManagedEnvironment::factory()->onboarding()->create();
    [$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

    createOnboardingDraft([
        'workspace' => $tenant->workspace,
        'tenant' => $tenant,
        'started_by' => $user,
        'updated_by' => $user,
        'state' => [
            'entra_tenant_id' => (string) $tenant->managed_environment_id,
            'tenant_name' => (string) $tenant->name,
        ],
    ]);

    $this->actingAs($user);
    Filament::setTenant($tenant, true);

    $component = Livewire::test(EditManagedEnvironment::class, ['record' => $tenant->getRouteKey()])
        ->assertSee('Related context')
        ->assertSee('Open environment detail')
        ->assertSee('Resume onboarding');

    expect(editTenantHeaderPrimaryNames($component))->toBe([])
        ->and(editTenantHeaderGroupLabels($component))->toBe([]);
});

it('keeps tenant lifecycle mutations available under the lifecycle header group with confirmation intact', function (): void {
    $tenant = ManagedEnvironment::factory()->active()->create();
    [$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner');

    $this->actingAs($user);
    Filament::setTenant($tenant, true);

    $component = Livewire::test(EditManagedEnvironment::class, ['record' => $tenant->getRouteKey()])
        ->assertActionVisible('archive')
        ->assertActionEnabled('archive')
        ->assertActionExists('archive', fn (Action $action): bool => $action->isConfirmationRequired());

    expect(editTenantHeaderPrimaryNames($component))->toBe([])
        ->and(editTenantHeaderGroupLabels($component))->toBe(['Lifecycle']);
});
