<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\ManagedEnvironment;
use App\Models\ManagedEnvironmentOnboardingSession;
use App\Models\User;
use App\Models\Workspace;
use App\Support\Onboarding\OnboardingCheckpoint;
use App\Support\Onboarding\OnboardingLifecycleState;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<ManagedEnvironmentOnboardingSession>
 */
class ManagedEnvironmentOnboardingSessionFactory extends Factory
{
    protected $model = ManagedEnvironmentOnboardingSession::class;

    public function definition(): array
    {
        $entraTenantId = fake()->uuid();
        $environmentName = fake()->company();

        return [
            'workspace_id' => Workspace::factory(),
            'managed_environment_id' => null,
            'entra_tenant_id' => $entraTenantId,
            'current_step' => 'identify',
            'state' => [
                'entra_tenant_id' => $entraTenantId,
                'environment_name' => $environmentName,
                'environment' => 'prod',
            ],
            'started_by_user_id' => User::factory(),
            'updated_by_user_id' => User::factory(),
            'version' => 1,
            'lifecycle_state' => OnboardingLifecycleState::Draft->value,
            'current_checkpoint' => OnboardingCheckpoint::Identify->value,
            'last_completed_checkpoint' => null,
            'reason_code' => null,
            'blocking_reason_code' => null,
            'completed_at' => null,
            'cancelled_at' => null,
        ];
    }

    public function forWorkspace(Workspace $workspace): static
    {
        return $this->state(fn (): array => [
            'workspace_id' => (int) $workspace->getKey(),
        ]);
    }

    public function forTenant(ManagedEnvironment $tenant): static
    {
        return $this->state(fn (array $attributes): array => [
            'workspace_id' => (int) $tenant->workspace_id,
            'managed_environment_id' => (int) $tenant->getKey(),
            'entra_tenant_id' => (string) $tenant->managed_environment_id,
            'state' => array_merge(is_array($attributes['state'] ?? null) ? $attributes['state'] : [], [
                'managed_environment_id' => (int) $tenant->getKey(),
                'entra_tenant_id' => (string) $tenant->managed_environment_id,
                'environment_name' => (string) $tenant->name,
                'environment' => (string) ($tenant->environment ?? 'prod'),
            ]),
        ]);
    }

    public function startedBy(User $user): static
    {
        return $this->state(fn (): array => [
            'started_by_user_id' => (int) $user->getKey(),
        ]);
    }

    public function updatedBy(User $user): static
    {
        return $this->state(fn (): array => [
            'updated_by_user_id' => (int) $user->getKey(),
        ]);
    }

    public function withProviderConnection(int $providerConnectionId): static
    {
        return $this->state(fn (array $attributes): array => [
            'current_step' => 'connection',
            'current_checkpoint' => OnboardingCheckpoint::ConnectProvider->value,
            'last_completed_checkpoint' => OnboardingCheckpoint::Identify->value,
            'state' => array_merge(is_array($attributes['state'] ?? null) ? $attributes['state'] : [], [
                'provider_connection_id' => $providerConnectionId,
            ]),
        ]);
    }

    public function withVerificationRun(int $operationRunId): static
    {
        return $this->state(fn (array $attributes): array => [
            'current_step' => 'verify',
            'current_checkpoint' => OnboardingCheckpoint::VerifyAccess->value,
            'last_completed_checkpoint' => OnboardingCheckpoint::ConnectProvider->value,
            'state' => array_merge(is_array($attributes['state'] ?? null) ? $attributes['state'] : [], [
                'verification_operation_run_id' => $operationRunId,
            ]),
        ]);
    }

    public function reviewReady(): static
    {
        return $this->state(fn (array $attributes): array => [
            'current_step' => 'bootstrap',
            'lifecycle_state' => OnboardingLifecycleState::ReadyForActivation->value,
            'current_checkpoint' => OnboardingCheckpoint::CompleteActivate->value,
            'last_completed_checkpoint' => OnboardingCheckpoint::VerifyAccess->value,
            'state' => array_merge(is_array($attributes['state'] ?? null) ? $attributes['state'] : [], [
                'bootstrap_operation_types' => [],
            ]),
        ]);
    }

    public function completed(): static
    {
        return $this->state(fn (): array => [
            'current_step' => 'complete',
            'lifecycle_state' => OnboardingLifecycleState::Completed->value,
            'current_checkpoint' => OnboardingCheckpoint::CompleteActivate->value,
            'last_completed_checkpoint' => OnboardingCheckpoint::CompleteActivate->value,
            'reason_code' => null,
            'blocking_reason_code' => null,
            'completed_at' => now(),
            'cancelled_at' => null,
        ]);
    }

    public function cancelled(): static
    {
        return $this->state(fn (): array => [
            'current_step' => 'cancelled',
            'lifecycle_state' => OnboardingLifecycleState::Cancelled->value,
            'current_checkpoint' => OnboardingCheckpoint::Identify->value,
            'last_completed_checkpoint' => null,
            'reason_code' => null,
            'blocking_reason_code' => null,
            'completed_at' => null,
            'cancelled_at' => now(),
        ]);
    }

    public function resumableForTenant(ManagedEnvironment $tenant): static
    {
        return $this->forTenant($tenant)->state(fn (): array => [
            'completed_at' => null,
            'cancelled_at' => null,
        ]);
    }
}
