<?php

declare(strict_types=1);

use App\Models\OperationRun;
use App\Models\ProviderConnection;
use App\Models\ManagedEnvironment;
use App\Services\Onboarding\OnboardingLifecycleService;
use App\Support\Onboarding\OnboardingCheckpoint;
use App\Support\Onboarding\OnboardingLifecycleState;
use App\Support\OperationRunOutcome;
use App\Support\OperationRunStatus;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('marks a draft as action required when the provider connection changed before verification reruns', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    $connection = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
    ]);

    $draft = createOnboardingDraft([
        'workspace' => $tenant->workspace,
        'tenant' => $tenant,
        'current_step' => 'verify',
        'state' => [
            'entra_tenant_id' => (string) $tenant->managed_environment_id,
            'environment_name' => (string) $tenant->name,
            'provider_connection_id' => (int) $connection->getKey(),
            'connection_recently_updated' => true,
        ],
    ]);

    $snapshot = app(OnboardingLifecycleService::class)->snapshot($draft);

    expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::ActionRequired)
        ->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::VerifyAccess)
        ->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::ConnectProvider)
        ->and($snapshot['reason_code'])->toBe('provider_connection_changed')
        ->and($snapshot['blocking_reason_code'])->toBe('provider_connection_changed');
});

it('treats a missing persisted provider connection as connect-provider state instead of verify state', function (): void {
    $tenant = ManagedEnvironment::factory()->create();

    $draft = createOnboardingDraft([
        'workspace' => $tenant->workspace,
        'tenant' => $tenant,
        'current_step' => 'verify',
        'state' => [
            'entra_tenant_id' => (string) $tenant->managed_environment_id,
            'environment_name' => (string) $tenant->name,
            'provider_connection_id' => 42,
        ],
    ]);

    $snapshot = app(OnboardingLifecycleService::class)->snapshot($draft);

    expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::Draft)
        ->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::ConnectProvider)
        ->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::Identify)
        ->and($snapshot['reason_code'])->toBeNull()
        ->and($snapshot['blocking_reason_code'])->toBeNull();
});

it('marks a draft as ready for activation when verification succeeded for the selected connection', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    $connection = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
    ]);

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

    $run = OperationRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
        'status' => OperationRunStatus::Completed->value,
        'outcome' => OperationRunOutcome::Succeeded->value,
        'context' => [
            'provider_connection_id' => (int) $connection->getKey(),
        ],
    ]);

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

    $service = app(OnboardingLifecycleService::class);
    $snapshot = $service->snapshot($draft->fresh());

    expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::ReadyForActivation)
        ->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::CompleteActivate)
        ->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::VerifyAccess)
        ->and($service->isReadyForActivation($draft->fresh()))->toBeTrue();
});

it('marks a draft as bootstrapping while a selected bootstrap run is still active', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    $connection = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
    ]);

    $verificationRun = OperationRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
        'status' => OperationRunStatus::Completed->value,
        'outcome' => OperationRunOutcome::Succeeded->value,
        'context' => [
            'provider_connection_id' => (int) $connection->getKey(),
        ],
    ]);

    $bootstrapRun = OperationRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
        'status' => OperationRunStatus::Running->value,
        'outcome' => OperationRunOutcome::Pending->value,
        'type' => 'inventory_sync',
        'context' => [
            'provider_connection_id' => (int) $connection->getKey(),
        ],
    ]);

    $draft = createOnboardingDraft([
        'workspace' => $tenant->workspace,
        'tenant' => $tenant,
        '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) $verificationRun->getKey(),
            'bootstrap_operation_types' => ['inventory_sync'],
            'bootstrap_operation_runs' => [
                'inventory_sync' => (int) $bootstrapRun->getKey(),
            ],
        ],
    ]);

    $service = app(OnboardingLifecycleService::class);
    $snapshot = $service->snapshot($draft);

    expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::Bootstrapping)
        ->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::Bootstrap)
        ->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::VerifyAccess)
        ->and($service->hasActiveCheckpoint($draft))->toBeTrue();
});

it('marks a draft as action required when a selected bootstrap run fails', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    $connection = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
    ]);

    $verificationRun = OperationRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
        'status' => OperationRunStatus::Completed->value,
        'outcome' => OperationRunOutcome::Succeeded->value,
        'context' => [
            'provider_connection_id' => (int) $connection->getKey(),
        ],
    ]);

    $bootstrapRun = OperationRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
        'status' => OperationRunStatus::Completed->value,
        'outcome' => OperationRunOutcome::Failed->value,
        'type' => 'inventory_sync',
        'context' => [
            'provider_connection_id' => (int) $connection->getKey(),
        ],
    ]);

    $draft = createOnboardingDraft([
        'workspace' => $tenant->workspace,
        'tenant' => $tenant,
        '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) $verificationRun->getKey(),
            'bootstrap_operation_types' => ['inventory_sync'],
            'bootstrap_operation_runs' => [
                'inventory_sync' => (int) $bootstrapRun->getKey(),
            ],
        ],
    ]);

    $snapshot = app(OnboardingLifecycleService::class)->snapshot($draft);

    expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::ActionRequired)
        ->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::Bootstrap)
        ->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::VerifyAccess)
        ->and($snapshot['reason_code'])->toBe('bootstrap_failed')
        ->and($snapshot['blocking_reason_code'])->toBe('bootstrap_failed');
});

it('applies the canonical lifecycle fields and normalizes the version floor', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    $connection = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
    ]);

    $draft = createOnboardingDraft([
        'workspace' => $tenant->workspace,
        'tenant' => $tenant,
        'version' => 0,
        'lifecycle_state' => OnboardingLifecycleState::Draft->value,
        'current_checkpoint' => OnboardingCheckpoint::Identify->value,
        'last_completed_checkpoint' => null,
        'current_step' => 'verify',
        'state' => [
            'entra_tenant_id' => (string) $tenant->managed_environment_id,
            'environment_name' => (string) $tenant->name,
            'provider_connection_id' => (int) $connection->getKey(),
            'connection_recently_updated' => true,
        ],
    ]);

    $changed = app(OnboardingLifecycleService::class)->applySnapshot($draft, false);

    expect($changed)->toBeTrue()
        ->and($draft->version)->toBe(1)
        ->and($draft->lifecycle_state)->toBe(OnboardingLifecycleState::ActionRequired)
        ->and($draft->current_checkpoint)->toBe(OnboardingCheckpoint::VerifyAccess)
        ->and($draft->reason_code)->toBe('provider_connection_changed');
});

it('keeps linked archived tenants loaded while suppressing onboarding resume affordances', function (): void {
    $tenant = ManagedEnvironment::factory()->archived()->create();

    $draft = createOnboardingDraft([
        'workspace' => $tenant->workspace,
        'tenant' => $tenant,
        'state' => [
            'entra_tenant_id' => (string) $tenant->managed_environment_id,
            'environment_name' => (string) $tenant->name,
        ],
    ]);

    $draft = $draft->fresh()->load('managedEnvironment');
    $service = app(OnboardingLifecycleService::class);

    expect($draft->managedEnvironment)->toBeInstanceOf(ManagedEnvironment::class)
        ->and($draft->managedEnvironment?->trashed())->toBeTrue()
        ->and($draft->isWorkflowResumable())->toBeTrue()
        ->and($service->canResumeDraft($draft))->toBeFalse();
});
