<?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\OperationRunOutcome;
use App\Support\OperationRunStatus;
use App\Support\Verification\VerificationReportWriter;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Support\Facades\Queue;
use Livewire\Livewire;

it('denies activation to non-owners even when verification succeeded', function (): void {
    Queue::fake();

    $workspace = Workspace::factory()->create();
    $user = User::factory()->create();

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

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

    $entraTenantId = '88888888-8888-8888-8888-888888888888';

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

    $component->call('identifyManagedEnvironment', [
        'entra_tenant_id' => $entraTenantId,
        'environment' => 'prod',
        'name' => 'Acme',
    ]);

    $tenant = ManagedEnvironment::query()->where('slug', $entraTenantId)->firstOrFail();
    $connection = ProviderConnection::factory()->platform()->consentGranted()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'managed_environment_id' => (int) $tenant->getKey(),
        'provider' => 'microsoft',
        'entra_tenant_id' => $entraTenantId,
        'display_name' => 'Acme connection',
        'is_default' => true,
        'consent_status' => 'granted',
    ]);

    $run = OperationRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $workspace->getKey(),
        'user_id' => (int) $user->getKey(),
        'type' => 'provider.connection.check',
        'status' => OperationRunStatus::Completed->value,
        'outcome' => OperationRunOutcome::Succeeded->value,
        'context' => [
            'provider' => 'microsoft',
            'module' => 'health_check',
            'provider_connection_id' => (int) $connection->getKey(),
            'target_scope' => [
                'entra_tenant_id' => $connection->entra_tenant_id,
            ],
        ],
    ]);

    $session = ManagedEnvironmentOnboardingSession::query()
        ->where('workspace_id', (int) $workspace->getKey())
        ->where('entra_tenant_id', $entraTenantId)
        ->firstOrFail();

    $session->forceFill([
        'state' => array_merge($session->state ?? [], [
            'provider_connection_id' => (int) $connection->getKey(),
            'verification_operation_run_id' => (int) $run->getKey(),
        ]),
    ])->save();

    $component->set('onboardingSession', $session->fresh());
    $component->set('selectedProviderConnectionId', (int) $connection->getKey());

    $component
        ->call('completeOnboarding')
        ->assertStatus(403);

    $tenant->refresh();
    expect($tenant->status)->not->toBe(ManagedEnvironment::STATUS_ACTIVE);
});

it('requires an override reason when verification is blocked and records an audit event when overridden', function (): void {
    Queue::fake();

    $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());

    $entraTenantId = '99999999-9999-9999-9999-999999999999';

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

    $component->call('identifyManagedEnvironment', [
        'entra_tenant_id' => $entraTenantId,
        'environment' => 'prod',
        'name' => 'Acme',
    ]);

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

    $component->call('startVerification');

    $tenant = ManagedEnvironment::query()->where('slug', $entraTenantId)->firstOrFail();

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

    $run->update([
        'status' => OperationRunStatus::Completed->value,
        'outcome' => OperationRunOutcome::Failed->value,
    ]);

    $component
        ->set('data.override_blocked', true)
        ->set('data.override_reason', '')
        ->call('completeOnboarding')
        ->assertHasErrors(['data.override_reason']);

    $component
        ->set('data.override_blocked', true)
        ->set('data.override_reason', 'Temporary exception approved by owner')
        ->call('completeOnboarding');

    $tenant->refresh();
    expect($tenant->status)->toBe(ManagedEnvironment::STATUS_ACTIVE);

    expect(AuditLog::query()
        ->where('workspace_id', (int) $workspace->getKey())
        ->where('action', 'managed_environment_onboarding.activation')
        ->exists())->toBeTrue()
        ->and(AuditLog::query()
            ->where('workspace_id', (int) $workspace->getKey())
            ->where('action', 'managed_environment_onboarding.activation_override_used')
            ->exists())->toBeTrue();
});

it('requires an override when the stored verification report is blocked even if the run outcome says succeeded', function (): void {
    Queue::fake();

    $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());

    $entraTenantId = '12121212-3434-5656-7878-909090909090';

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

    $component->call('identifyManagedEnvironment', [
        'entra_tenant_id' => $entraTenantId,
        'environment' => 'prod',
        'name' => 'Blocked By Report',
    ]);

    $tenant = ManagedEnvironment::query()->where('slug', $entraTenantId)->firstOrFail();

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

    $run = OperationRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $workspace->getKey(),
        'user_id' => (int) $user->getKey(),
        'type' => 'provider.connection.check',
        'status' => OperationRunStatus::Completed->value,
        'outcome' => OperationRunOutcome::Succeeded->value,
        'context' => [
            'provider' => 'microsoft',
            'module' => 'health_check',
            'provider_connection_id' => (int) $connection->getKey(),
            'target_scope' => [
                'entra_tenant_id' => $connection->entra_tenant_id,
            ],
            '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' => [],
                ],
            ]),
        ],
    ]);

    $session = ManagedEnvironmentOnboardingSession::query()
        ->where('workspace_id', (int) $workspace->getKey())
        ->where('entra_tenant_id', $entraTenantId)
        ->firstOrFail();

    $session->forceFill([
        'state' => array_merge($session->state ?? [], [
            'provider_connection_id' => (int) $connection->getKey(),
            'verification_operation_run_id' => (int) $run->getKey(),
        ]),
    ])->save();

    $component->set('onboardingSession', $session->fresh());
    $component->set('selectedProviderConnectionId', (int) $connection->getKey());

    $component->call('completeOnboarding');

    $tenant->refresh();
    expect($tenant->status)->not->toBe(ManagedEnvironment::STATUS_ACTIVE);

    $component
        ->set('data.override_blocked', true)
        ->set('data.override_reason', 'Owner approved temporary exception')
        ->call('completeOnboarding');

    $tenant->refresh();
    expect($tenant->status)->toBe(ManagedEnvironment::STATUS_ACTIVE);
});

it('does not activate or write activation audit history when the workspace changes after mount', function (): void {
    Queue::fake();

    $workspaceA = Workspace::factory()->create();
    $workspaceB = Workspace::factory()->create();
    $user = User::factory()->create();

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

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

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

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

    $run = OperationRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $workspaceA->getKey(),
        'user_id' => (int) $user->getKey(),
        'type' => 'provider.connection.check',
        'status' => OperationRunStatus::Completed->value,
        'outcome' => OperationRunOutcome::Succeeded->value,
        'context' => [
            'provider' => 'microsoft',
            'module' => 'health_check',
            'provider_connection_id' => (int) $connection->getKey(),
            'target_scope' => [
                'entra_tenant_id' => (string) $tenant->managed_environment_id,
            ],
        ],
    ]);

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

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

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

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

    $component->call('completeOnboarding')->assertNotFound();

    $tenant->refresh();

    expect($tenant->status)->toBe(ManagedEnvironment::STATUS_ONBOARDING)
        ->and(AuditLog::query()
            ->where('workspace_id', (int) $workspaceA->getKey())
            ->whereIn('action', [
                'managed_environment_onboarding.activation',
                'managed_environment_onboarding.activation_override_used',
            ])
            ->exists())->toBeFalse();
});
