<?php

declare(strict_types=1);

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('nova_cards', function (Blueprint $table): void {
            if (! Schema::hasColumn('nova_cards', 'scheduled_for')) {
                $table->timestamp('scheduled_for')->nullable()->after('published_at');
            }

            if (! Schema::hasColumn('nova_cards', 'scheduling_timezone')) {
                $table->string('scheduling_timezone', 64)->nullable()->after('scheduled_for');
            }
        });

        Schema::table('nova_cards', function (Blueprint $table): void {
            $table->index(['status', 'scheduled_for'], 'nova_cards_status_scheduled_for_idx');
        });
    }

    public function down(): void
    {
        Schema::table('nova_cards', function (Blueprint $table): void {
            $table->dropIndex('nova_cards_status_scheduled_for_idx');

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

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