<?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('deals', function (Blueprint $t) {
            if (! Schema::hasColumn('deals', 'urgent')) {
                $t->boolean('urgent')->default(false)->after('source');
            }
            if (! Schema::hasColumn('deals', 'quote_sent_at')) {
                $t->timestamp('quote_sent_at')->nullable()->after('urgent');
            }
            if (! Schema::hasColumn('deals', 'quote_status')) {
                $t->string('quote_status', 16)->nullable()->after('quote_sent_at');
            }
            if (! Schema::hasColumn('deals', 'quote_seen_at')) {
                $t->timestamp('quote_seen_at')->nullable()->after('quote_status');
            }
            if (! Schema::hasColumn('deals', 'scheduled_at')) {
                $t->timestamp('scheduled_at')->nullable()->after('quote_seen_at');
            }
            if (! Schema::hasColumn('deals', 'bay')) {
                $t->string('bay', 32)->nullable()->after('scheduled_at');
            }
            if (! Schema::hasColumn('deals', 'confirmed_at')) {
                $t->timestamp('confirmed_at')->nullable()->after('bay');
            }
            if (! Schema::hasColumn('deals', 'confirmed_via')) {
                $t->string('confirmed_via', 16)->nullable()->after('confirmed_at');
            }
            if (! Schema::hasColumn('deals', 'last_action_at')) {
                $t->timestamp('last_action_at')->nullable()->after('confirmed_via');
            }
        });
    }

    public function down(): void
    {
        Schema::table('deals', function (Blueprint $t) {
            foreach (['urgent', 'quote_sent_at', 'quote_status', 'quote_seen_at', 'scheduled_at', 'bay', 'confirmed_at', 'confirmed_via', 'last_action_at'] as $col) {
                if (Schema::hasColumn('deals', $col)) {
                    $t->dropColumn($col);
                }
            }
        });
    }
};
