<?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): void {
            if (!Schema::hasColumn('dealers', 'baseline_version_applied')) {
                $table->string('baseline_version_applied', 64)->nullable()->after('provisioning_status');
            }

            if (!Schema::hasColumn('dealers', 'baseline_last_applied_at')) {
                $table->timestamp('baseline_last_applied_at')->nullable()->after('baseline_version_applied');
            }
        });
    }

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

        Schema::table('dealers', function (Blueprint $table): void {
            if (Schema::hasColumn('dealers', 'baseline_last_applied_at')) {
                $table->dropColumn('baseline_last_applied_at');
            }

            if (Schema::hasColumn('dealers', 'baseline_version_applied')) {
                $table->dropColumn('baseline_version_applied');
            }
        });
    }
};

