<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Database\Migrations\TenantMigration;

return new class extends TenantMigration
{
    public function up()
    {
        Schema::create('email_scheduled_recipients', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('email_scheduled_id');
            $table->unsignedBigInteger('user_id');
            $table->string('email_address');
            $table->enum('status', ['pending', 'sent', 'failed', 'bounced'])->default('pending');
            $table->text('error_message')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->timestamps();

            $table->foreign('email_scheduled_id')->references('id')->on('email_scheduled')->onDelete('cascade');
            $table->foreign('user_id')->references('id')->on('users');
            $table->unique(['email_scheduled_id', 'user_id']);
        });
    }

    public function down()
    {
        Schema::dropIfExists('email_scheduled_recipients');
    }
};
