<?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', 'logo_path')) {
                $table->string('logo_path')->nullable()->after('provisioning_status');
            }
            if (! Schema::hasColumn('dealers', 'favicon_path')) {
                $table->string('favicon_path')->nullable()->after('logo_path');
            }
            if (! Schema::hasColumn('dealers', 'theme_mode')) {
                $table->string('theme_mode', 20)->nullable()->after('favicon_path');
            }
            if (! Schema::hasColumn('dealers', 'theme_preset_key')) {
                $table->string('theme_preset_key', 80)->nullable()->after('theme_mode');
            }
            if (! Schema::hasColumn('dealers', 'nav_color')) {
                $table->string('nav_color', 32)->nullable()->after('theme_preset_key');
            }
            if (! Schema::hasColumn('dealers', 'accent_color')) {
                $table->string('accent_color', 32)->nullable()->after('nav_color');
            }
            if (! Schema::hasColumn('dealers', 'accent_color_2')) {
                $table->string('accent_color_2', 32)->nullable()->after('accent_color');
            }
            if (! Schema::hasColumn('dealers', 'background_color')) {
                $table->string('background_color', 32)->nullable()->after('accent_color_2');
            }
        });
    }

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

        Schema::table('dealers', function (Blueprint $table) {
            $columns = [
                'background_color',
                'accent_color_2',
                'accent_color',
                'nav_color',
                'theme_preset_key',
                'theme_mode',
                'favicon_path',
                'logo_path',
            ];

            foreach ($columns as $column) {
                if (Schema::hasColumn('dealers', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};

