<?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('email_logs', function (Blueprint $table) {
            if (Schema::hasColumn('email_logs', 'message_id')) {
                $table->dropUnique(['message_id']);
                $table->dropColumn('message_id');
            }
            // Make to_email nullable to allow blank when multiple recipients
            $table->string('to_email')->nullable()->change();
        });
    }

    public function down(): void
    {
        Schema::table('email_logs', function (Blueprint $table) {
            // Recreate message_id as nullable unique if needed
            if (! Schema::hasColumn('email_logs', 'message_id')) {
                $table->string('message_id')->nullable()->unique();
            }
            // Revert to_email to not nullable
            $table->string('to_email')->nullable(false)->change();
        });
    }
};
