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

            if (! Schema::hasColumn('users', 'verification_send_count_24h')) {
                $table->unsignedInteger('verification_send_count_24h')->default(0)->after('last_verification_sent_at');
            }

            if (! Schema::hasColumn('users', 'verification_send_window_started_at')) {
                $table->timestamp('verification_send_window_started_at')->nullable()->after('verification_send_count_24h');
            }
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table): void {
            if (Schema::hasColumn('users', 'verification_send_window_started_at')) {
                $table->dropColumn('verification_send_window_started_at');
            }

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

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