<?php

declare(strict_types=1);

use App\Filament\Resources\ManagedEnvironmentResource;
use App\Filament\Resources\ManagedEnvironmentResource\Pages\ListManagedEnvironments;
use App\Filament\Resources\ManagedEnvironmentResource\Pages\ViewManagedEnvironment;
use App\Models\AuditLog;
use App\Models\ManagedEnvironment;
use App\Services\Audit\WorkspaceAuditLogger;
use App\Support\Audit\AuditActionId;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Actions\Action;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

it('shows resume onboarding instead of archive for draft and onboarding tenants on list and detail surfaces', function (\Closure $tenantFactory): void {
    $tenant = $tenantFactory();
    [$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,
        ],
    ]);

    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);

    Livewire::actingAs($user)
        ->test(ListManagedEnvironments::class)
        ->assertTableActionVisible('related_onboarding', $tenant)
        ->assertTableActionExists('related_onboarding', fn (Action $action): bool => $action->getLabel() === 'Resume onboarding', $tenant)
        ->assertTableActionHidden('archive', $tenant)
        ->assertTableActionHidden('restore', $tenant);

    Filament::setTenant(null, true);

    Livewire::actingAs($user)
        ->test(ViewManagedEnvironment::class, ['record' => $tenant->getRouteKey()])
        ->assertActionHidden('archive')
        ->assertActionHidden('restore');

    expect(collect(ManagedEnvironmentResource::tenantViewContextEntries($tenant))
        ->firstWhere('key', 'related_onboarding')['value'] ?? null)
        ->toBe('Resume onboarding');
})->with([
    'draft' => [fn (): ManagedEnvironment => ManagedEnvironment::factory()->draft()->create()],
    'onboarding' => [fn (): ManagedEnvironment => ManagedEnvironment::factory()->onboarding()->create()],
]);

it('shows archive only for active tenants on list and detail surfaces', function (): void {
    $tenant = ManagedEnvironment::factory()->active()->create();
    [$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner');

    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);

    Livewire::actingAs($user)
        ->test(ListManagedEnvironments::class)
        ->assertTableActionVisible('archive', $tenant)
        ->assertTableActionHidden('restore', $tenant)
        ->assertTableActionHidden('related_onboarding', $tenant);

    Filament::setTenant(null, true);

    Livewire::actingAs($user)
        ->test(ViewManagedEnvironment::class, ['record' => $tenant->getRouteKey()])
        ->assertActionVisible('archive')
        ->assertActionHidden('restore');
});

it('shows restore only for archived tenants on list and detail surfaces', function (): void {
    $tenant = ManagedEnvironment::factory()->archived()->create();
    [$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);

    Livewire::actingAs($user)
        ->test(ListManagedEnvironments::class)
        ->assertTableActionVisible('restore', $tenant)
        ->assertTableActionHidden('archive', $tenant)
        ->assertTableActionHidden('related_onboarding', $tenant);

    Filament::setTenant(null, true);

    Livewire::actingAs($user)
        ->test(ViewManagedEnvironment::class, ['record' => $tenant->getRouteKey()])
        ->assertActionVisible('restore')
        ->assertActionHidden('archive');

    expect(collect(ManagedEnvironmentResource::tenantViewContextEntries($tenant))->pluck('key')->all())
        ->not->toContain('related_onboarding');
});

it('shows verification only for active tenants on administrative list and detail surfaces', function (
    \Closure $tenantFactory,
    bool $shouldBeVisible,
): void {
    $tenant = $tenantFactory();
    [$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);

    $list = Livewire::actingAs($user)->test(ListManagedEnvironments::class);

    if ($shouldBeVisible) {
        $list->assertTableActionVisible('verify', $tenant);
    } else {
        $list->assertTableActionHidden('verify', $tenant);
    }

    Filament::setTenant(null, true);

    $view = Livewire::actingAs($user)
        ->test(ViewManagedEnvironment::class, ['record' => $tenant->getRouteKey()]);

    if ($shouldBeVisible) {
        $view->assertActionVisible('verify');
    } else {
        $view->assertActionHidden('verify');
    }
})->with([
    'draft' => [fn (): ManagedEnvironment => ManagedEnvironment::factory()->draft()->create(), false],
    'onboarding' => [fn (): ManagedEnvironment => ManagedEnvironment::factory()->onboarding()->create(), false],
    'active' => [fn (): ManagedEnvironment => ManagedEnvironment::factory()->active()->create(), true],
    'archived' => [fn (): ManagedEnvironment => ManagedEnvironment::factory()->archived()->create(), false],
]);

it('keeps lifecycle actions visible but disabled for in-scope members without mutation capability', function (): void {
    $tenant = ManagedEnvironment::factory()->active()->create();
    [$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'manager');

    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);

    Livewire::actingAs($user)
        ->test(ListManagedEnvironments::class)
        ->assertTableActionVisible('archive', $tenant)
        ->assertTableActionDisabled('archive', $tenant);
});

it('shows capability-denied actions as disabled but keeps lifecycle-denied actions hidden', function (): void {
    $activeEnvironment = ManagedEnvironment::factory()->active()->create([
        'name' => 'Capability Denied Active ManagedEnvironment',
    ]);
    [$user, $activeEnvironment] = createUserWithTenant(tenant: $activeEnvironment, role: 'manager', ensureDefaultMicrosoftProviderConnection: false);

    $onboardingTenant = ManagedEnvironment::factory()->onboarding()->create([
        'workspace_id' => (int) $activeEnvironment->workspace_id,
        'name' => 'Lifecycle Denied Onboarding ManagedEnvironment',
    ]);

    createUserWithTenant(
        tenant: $onboardingTenant,
        user: $user,
        role: 'manager',
        workspaceRole: 'manager',
        ensureDefaultMicrosoftProviderConnection: false,
    );

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

    session()->put(WorkspaceContext::SESSION_KEY, (int) $activeEnvironment->workspace_id);

    Livewire::actingAs($user)
        ->test(ListManagedEnvironments::class)
        ->assertTableActionVisible('archive', $activeEnvironment)
        ->assertTableActionDisabled('archive', $activeEnvironment)
        ->assertTableActionHidden('archive', $onboardingTenant)
        ->assertTableActionVisible('related_onboarding', $onboardingTenant);

    Filament::setTenant(null, true);

    Livewire::actingAs($user)
        ->test(ViewManagedEnvironment::class, ['record' => $activeEnvironment->getRouteKey()])
        ->assertActionVisible('archive')
        ->assertActionDisabled('archive');

    Livewire::actingAs($user)
        ->test(ViewManagedEnvironment::class, ['record' => $onboardingTenant->getRouteKey()])
        ->assertActionHidden('archive')
        ->assertActionHidden('restore');

    expect(collect(ManagedEnvironmentResource::tenantViewContextEntries($onboardingTenant))->pluck('key')->all())
        ->toContain('related_onboarding');
});

it('returns 404 on tenant detail routes for non-members regardless of lifecycle state', function (\Closure $tenantFactory): void {
    $tenant = $tenantFactory();
    [$user] = createUserWithTenant(role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

    $this->actingAs($user)
        ->get(ManagedEnvironmentResource::getUrl('view', ['record' => $tenant]))
        ->assertNotFound();
})->with([
    'draft' => [fn (): ManagedEnvironment => ManagedEnvironment::factory()->draft()->create()],
    'onboarding' => [fn (): ManagedEnvironment => ManagedEnvironment::factory()->onboarding()->create()],
    'active' => [fn (): ManagedEnvironment => ManagedEnvironment::factory()->active()->create()],
    'archived' => [fn (): ManagedEnvironment => ManagedEnvironment::factory()->archived()->create()],
]);

it('keeps tenant detail lifecycle actions bound to the viewed record instead of the selected header tenant', function (): void {
    $selectedTenant = ManagedEnvironment::factory()->active()->create();
    [$user, $selectedTenant] = createUserWithTenant(tenant: $selectedTenant, role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

    $archivedTenant = ManagedEnvironment::factory()->archived()->create([
        'workspace_id' => (int) $selectedTenant->workspace_id,
    ]);

    createUserWithTenant(
        tenant: $archivedTenant,
        user: $user,
        role: 'owner',
        workspaceRole: 'owner',
        ensureDefaultMicrosoftProviderConnection: false,
    );

    Filament::setTenant($selectedTenant, true);

    Livewire::actingAs($user)
        ->test(ViewManagedEnvironment::class, ['record' => $archivedTenant->getRouteKey()])
        ->assertActionVisible('restore')
        ->assertActionHidden('archive');

    expect(collect(ManagedEnvironmentResource::tenantViewContextEntries($archivedTenant))->pluck('key')->all())
        ->not->toContain('related_onboarding');
});

it('refuses lifecycle-invalid archive and restore mutations without changing tenant state', function (): void {
    $activeEnvironment = ManagedEnvironment::factory()->active()->create();
    [$user, $activeEnvironment] = createUserWithTenant(tenant: $activeEnvironment, role: 'owner');

    $onboardingTenant = ManagedEnvironment::factory()->onboarding()->create([
        'workspace_id' => (int) $activeEnvironment->workspace_id,
    ]);

    createUserWithTenant(
        tenant: $onboardingTenant,
        user: $user,
        role: 'owner',
        workspaceRole: 'owner',
        ensureDefaultMicrosoftProviderConnection: false,
    );

    $this->actingAs($user);

    $auditLogger = app(WorkspaceAuditLogger::class);

    ManagedEnvironmentResource::restoreTenant($activeEnvironment, $auditLogger);
    ManagedEnvironmentResource::archiveTenant($onboardingTenant, $auditLogger, 'Trying to archive an onboarding tenant should be rejected.');

    $activeEnvironment->refresh();
    $onboardingTenant->refresh();

    expect($activeEnvironment->trashed())->toBeFalse()
        ->and($onboardingTenant->trashed())->toBeFalse()
        ->and($onboardingTenant->status)->toBe(ManagedEnvironment::STATUS_ONBOARDING)
        ->and(AuditLog::query()
            ->whereIn('action', [
                AuditActionId::TenantArchived->value,
                AuditActionId::TenantRestored->value,
            ])
            ->whereIn('resource_id', [
                (string) $activeEnvironment->getKey(),
                (string) $onboardingTenant->getKey(),
            ])
            ->exists())->toBeFalse();
});
