<?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
    {
        if (!Schema::hasTable('dealers')) {
            return;
        }

        Schema::table('dealers', function (Blueprint $table) {
            if (!Schema::hasColumn('dealers', 'provisioned_at')) {
                $table->timestamp('provisioned_at')->nullable()->after('is_active');
            }

            if (!Schema::hasColumn('dealers', 'provisioning_status')) {
                $table->string('provisioning_status', 32)->default('pending')->after('provisioned_at');
            }

            if (!Schema::hasColumn('dealers', 'provisioning_error')) {
                $table->text('provisioning_error')->nullable()->after('provisioning_status');
            }
        });
    }

    public function down(): void
    {
        if (!Schema::hasTable('dealers')) {
            return;
        }

        Schema::table('dealers', function (Blueprint $table) {
            if (Schema::hasColumn('dealers', 'provisioning_error')) {
                $table->dropColumn('provisioning_error');
            }
            if (Schema::hasColumn('dealers', 'provisioning_status')) {
                $table->dropColumn('provisioning_status');
            }
            if (Schema::hasColumn('dealers', 'provisioned_at')) {
                $table->dropColumn('provisioned_at');
            }
        });
    }
};

