<?php

declare(strict_types=1);

use App\Filament\Pages\Workspaces\ManagedEnvironmentOnboardingWizard;
use App\Models\AuditLog;
use App\Models\OperationRun;
use App\Models\ProviderConnection;
use App\Models\ManagedEnvironment;
use App\Models\ManagedEnvironmentOnboardingSession;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceMembership;
use App\Support\Audit\AuditActionId;
use App\Support\OperationRunOutcome;
use App\Support\OperationRunStatus;
use App\Support\Verification\VerificationReportWriter;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Actions\Testing\TestAction;
use Illuminate\Support\Facades\Bus;
use Livewire\Livewire;

it('records start and resume audit events for onboarding drafts', function (): void {
    $workspace = Workspace::factory()->create();
    $user = User::factory()->create();

    WorkspaceMembership::factory()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'user_id' => (int) $user->getKey(),
        'role' => 'owner',
    ]);

    session()->put(WorkspaceContext::SESSION_KEY, (int) $workspace->getKey());

    Livewire::actingAs($user)
        ->test(ManagedEnvironmentOnboardingWizard::class)
        ->call('identifyManagedEnvironment', [
            'entra_tenant_id' => '11111111-1111-1111-1111-111111111111',
            'environment' => 'prod',
            'name' => 'Audit ManagedEnvironment',
        ]);

    $firstDraft = ManagedEnvironmentOnboardingSession::query()
        ->where('workspace_id', (int) $workspace->getKey())
        ->where('entra_tenant_id', '11111111-1111-1111-1111-111111111111')
        ->firstOrFail();

    createOnboardingDraft([
        'workspace' => $workspace,
        'started_by' => $user,
        'updated_by' => $user,
        'entra_tenant_id' => '22222222-2222-2222-2222-222222222222',
        'state' => [
            'entra_tenant_id' => '22222222-2222-2222-2222-222222222222',
            'tenant_name' => 'Stability Draft',
        ],
    ]);

    Livewire::actingAs($user)
        ->test(ManagedEnvironmentOnboardingWizard::class)
        ->call('identifyManagedEnvironment', [
            'entra_tenant_id' => '11111111-1111-1111-1111-111111111111',
            'environment' => 'prod',
            'name' => 'Audit ManagedEnvironment',
        ])
        ->assertRedirect(route('admin.onboarding.draft', ['onboardingDraft' => $firstDraft->getKey()]));

    expect(AuditLog::query()
        ->where('workspace_id', (int) $workspace->getKey())
        ->where('action', AuditActionId::ManagedEnvironmentOnboardingStart->value)
        ->exists())->toBeTrue()
        ->and(AuditLog::query()
            ->where('workspace_id', (int) $workspace->getKey())
            ->where('action', AuditActionId::ManagedEnvironmentOnboardingResume->value)
            ->exists())->toBeTrue();
});

it('records explicit draft selection and draft update audit entries', function (): void {
    $workspace = Workspace::factory()->create();
    $user = User::factory()->create();

    WorkspaceMembership::factory()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'user_id' => (int) $user->getKey(),
        'role' => 'owner',
    ]);

    session()->put(WorkspaceContext::SESSION_KEY, (int) $workspace->getKey());

    $draft = createOnboardingDraft([
        'workspace' => $workspace,
        'started_by' => $user,
        'updated_by' => $user,
        'state' => [
            'entra_tenant_id' => '33333333-3333-3333-3333-333333333333',
            'tenant_name' => 'Selectable Draft',
        ],
    ]);

    createOnboardingDraft([
        'workspace' => $workspace,
        'started_by' => $user,
        'updated_by' => $user,
        'state' => [
            'entra_tenant_id' => '44444444-4444-4444-4444-444444444444',
            'tenant_name' => 'Other Draft',
        ],
    ]);

    Livewire::actingAs($user)
        ->test(ManagedEnvironmentOnboardingWizard::class)
        ->callAction(
            TestAction::make('resume_draft_'.$draft->getKey())
                ->schemaComponent('draft_picker_actions_'.$draft->getKey())
        )
        ->assertRedirect(route('admin.onboarding.draft', ['onboardingDraft' => $draft->getKey()]));

    expect(AuditLog::query()
        ->where('workspace_id', (int) $workspace->getKey())
        ->where('action', AuditActionId::ManagedEnvironmentOnboardingDraftSelected->value)
        ->where('resource_id', (string) $draft->getKey())
        ->exists())->toBeTrue();

    $component = Livewire::actingAs($user)->test(ManagedEnvironmentOnboardingWizard::class, [
        'onboardingDraft' => (int) $draft->getKey(),
    ]);

    $touchStep = \Closure::bind(function (string $step): void {
        $this->touchOnboardingSessionStep($step);
    }, $component->instance(), $component->instance()::class);

    $touchStep('verify');

    expect(AuditLog::query()
        ->where('workspace_id', (int) $workspace->getKey())
        ->where('action', AuditActionId::ManagedEnvironmentOnboardingDraftUpdated->value)
        ->where('resource_id', (string) $draft->getKey())
        ->exists())->toBeTrue();
});

it('records provider, verification, bootstrap, and cancellation audit entries', function (): void {
    Bus::fake();

    $workspace = Workspace::factory()->create();
    $tenant = ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'status' => ManagedEnvironment::STATUS_ONBOARDING,
    ]);
    $user = User::factory()->create();

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

    $connection = ProviderConnection::factory()->platform()->consentGranted()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'managed_environment_id' => (int) $tenant->getKey(),
        'provider' => 'microsoft',
        'entra_tenant_id' => (string) $tenant->managed_environment_id,
        'display_name' => 'Audit Connection',
        'is_default' => true,
        'consent_status' => 'granted',
    ]);

    $draft = createOnboardingDraft([
        'workspace' => $workspace,
        'tenant' => $tenant,
        'started_by' => $user,
        'updated_by' => $user,
        'current_step' => 'connection',
        'state' => [
            'entra_tenant_id' => (string) $tenant->managed_environment_id,
            'tenant_name' => (string) $tenant->name,
            'provider_connection_id' => (int) $connection->getKey(),
        ],
    ]);

    session()->put(WorkspaceContext::SESSION_KEY, (int) $workspace->getKey());

    $component = Livewire::actingAs($user)->test(ManagedEnvironmentOnboardingWizard::class, [
        'onboardingDraft' => (int) $draft->getKey(),
    ]);

    $component->call('selectProviderConnection', (int) $connection->getKey());
    $component->call('startVerification');

    $draft->refresh();

    $verificationRunId = (int) ($draft->state['verification_operation_run_id'] ?? 0);
    $verificationRun = OperationRun::query()->findOrFail($verificationRunId);
    $verificationRun->update([
        'status' => OperationRunStatus::Completed->value,
        'outcome' => OperationRunOutcome::Succeeded->value,
        'context' => array_merge(is_array($verificationRun->context) ? $verificationRun->context : [], [
            'provider_connection_id' => (int) $connection->getKey(),
        ]),
    ]);

    $component->call('startBootstrap', ['inventory_sync']);

    $component
        ->mountAction('cancel_onboarding_draft')
        ->callMountedAction()
        ->assertNotified('Onboarding draft cancelled');

    expect(AuditLog::query()
        ->where('workspace_id', (int) $workspace->getKey())
        ->where('action', AuditActionId::ManagedEnvironmentOnboardingProviderConnectionChanged->value)
        ->exists())->toBeTrue()
        ->and(AuditLog::query()
            ->where('workspace_id', (int) $workspace->getKey())
            ->where('action', AuditActionId::ManagedEnvironmentOnboardingVerificationStart->value)
            ->exists())->toBeTrue()
        ->and(AuditLog::query()
            ->where('workspace_id', (int) $workspace->getKey())
            ->where('action', AuditActionId::ManagedEnvironmentOnboardingVerificationPersisted->value)
            ->exists())->toBeTrue()
        ->and(AuditLog::query()
            ->where('workspace_id', (int) $workspace->getKey())
            ->where('action', AuditActionId::ManagedEnvironmentOnboardingBootstrapStarted->value)
            ->exists())->toBeTrue()
        ->and(AuditLog::query()
            ->where('workspace_id', (int) $workspace->getKey())
            ->where('action', AuditActionId::ManagedEnvironmentOnboardingCancelled->value)
            ->exists())->toBeTrue();
});

it('records activation override audit entries when activation bypasses a blocked verification result', function (): void {
    $workspace = Workspace::factory()->create();
    $user = User::factory()->create();

    WorkspaceMembership::factory()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'user_id' => (int) $user->getKey(),
        'role' => 'owner',
    ]);

    session()->put(WorkspaceContext::SESSION_KEY, (int) $workspace->getKey());

    $component = Livewire::actingAs($user)->test(ManagedEnvironmentOnboardingWizard::class);

    $component->call('identifyManagedEnvironment', [
        'entra_tenant_id' => '55555555-5555-5555-5555-555555555555',
        'environment' => 'prod',
        'name' => 'Override ManagedEnvironment',
    ]);

    $component->call('createProviderConnection', [
        'display_name' => 'Override Connection',
        'client_id' => '00000000-0000-0000-0000-000000000000',
        'client_secret' => 'super-secret',
        'is_default' => true,
    ]);

    $component->call('startVerification');

    $tenant = ManagedEnvironment::query()->forTenant('55555555-5555-5555-5555-555555555555')->firstOrFail();

    $run = OperationRun::query()
        ->where('managed_environment_id', (int) $tenant->getKey())
        ->where('type', 'provider.connection.check')
        ->latest('id')
        ->firstOrFail();

    $run->update([
        'status' => OperationRunStatus::Completed->value,
        'outcome' => OperationRunOutcome::Failed->value,
        'context' => array_merge(is_array($run->context) ? $run->context : [], [
            'verification_report' => VerificationReportWriter::build('provider.connection.check', [
                [
                    'key' => 'permission_check',
                    'title' => 'Graph permissions',
                    'status' => 'fail',
                    'severity' => 'high',
                    'blocking' => true,
                    'reason_code' => 'permission_denied',
                    'message' => 'Missing required Graph permissions.',
                    'evidence' => [],
                    'next_steps' => [],
                ],
            ]),
        ]),
    ]);

    $component
        ->set('data.override_blocked', true)
        ->set('data.override_reason', 'Approved exception for onboarding continuity')
        ->call('completeOnboarding');

    expect(AuditLog::query()
        ->where('workspace_id', (int) $workspace->getKey())
        ->where('action', AuditActionId::ManagedEnvironmentOnboardingActivationOverrideUsed->value)
        ->exists())->toBeTrue()
        ->and(AuditLog::query()
            ->where('workspace_id', (int) $workspace->getKey())
            ->where('action', AuditActionId::ManagedEnvironmentOnboardingActivation->value)
            ->exists())->toBeTrue();
});
