<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('clients', function (Blueprint $table) {
            if (! Schema::hasColumn('clients', 'onboarding_status')) {
                $table->string('onboarding_status', 30)->default('not_started')->after('status');
            }

            if (! Schema::hasColumn('clients', 'onboarding_step')) {
                $table->string('onboarding_step', 50)->nullable()->after('onboarding_status');
            }

            if (! Schema::hasColumn('clients', 'onboarding_completed_at')) {
                $table->timestamp('onboarding_completed_at')->nullable()->after('onboarding_step');
            }

            if (! Schema::hasColumn('clients', 'onboarding_meta')) {
                $table->json('onboarding_meta')->nullable()->after('onboarding_completed_at');
            }
        });
    }

    public function down(): void
    {
        Schema::table('clients', function (Blueprint $table) {
            foreach (['onboarding_meta', 'onboarding_completed_at', 'onboarding_step', 'onboarding_status'] as $column) {
                if (Schema::hasColumn('clients', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
