<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (Schema::hasTable('managed_environment_onboarding_sessions')) {
            return;
        }

        Schema::create('managed_environment_onboarding_sessions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('workspace_id')->constrained()->cascadeOnDelete();
            $table->foreignId('managed_environment_id')->constrained()->cascadeOnDelete();

            $table->string('current_step')->nullable();
            $table->json('state')->nullable();

            $table->foreignId('started_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by_user_id')->nullable()->constrained('users')->nullOnDelete();

            $table->timestamp('completed_at')->nullable();
            $table->timestamps();

            $table->unique(['workspace_id', 'managed_environment_id']);
            $table->index(['managed_environment_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('managed_environment_onboarding_sessions');
    }
};
