<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('mail_queues', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('user_id')->nullable()->index();

            $table->string('type')->default('email');

            $table->string('to');
            $table->string('subject');

            $table->text('body')->nullable();

            $table->json('meta')->nullable();
            $table->string('status')->default('pending');
            $table->string('template');
            $table->string('locale')->default('en');

            $table->integer('tries')->default(0);
            $table->integer('max_tries')->default(3);

            $table->timestamp('available_at')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->text('error')->nullable();

            $table->timestamps();

            $table->index(['status', 'available_at']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('mail_queues');
    }
};
