<?php

use Illuminate\Database\Migrations\Migration;
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_messages', function (Blueprint $t) {
            $t->id();
            $t->string('subject');
            $t->longText('content_html');
            $t->enum('status', ['draft', 'scheduled', 'processing', 'sent', 'failed', 'canceled'])->default('draft')->index();
            $t->dateTime('schedule_at')->nullable()->index();
            $t->dateTime('sent_at')->nullable();
            $t->foreignId('created_by')->index();
            $t->timestamps();
        });

        Schema::create('email_message_recipients', function (Blueprint $t) {
            $t->id();
            $t->foreignId('email_message_id')->constrained('email_messages')->cascadeOnDelete();
            $t->foreignId('member_id')->nullable()->constrained('members')->nullOnDelete();
            $t->string('email_address');
            $t->enum('status', ['pending', 'sent', 'failed', 'bounced', 'skipped'])->default('pending')->index();
            $t->text('error_message')->nullable();
            $t->dateTime('sent_at')->nullable();
            $t->timestamps();

            $t->index(['email_message_id', 'status']);
        });

        Schema::create('email_message_attachments', function (Blueprint $t) {
            $t->id();
            $t->foreignId('email_message_id')->constrained('email_messages')->cascadeOnDelete();
            $t->string('disk')->default('public');
            $t->string('path');
            $t->string('name');
            $t->unsignedBigInteger('size')->nullable();
            $t->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('email_message_attachments');
        Schema::dropIfExists('email_message_recipients');
        Schema::dropIfExists('email_messages');
    }
};
