<?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::create('reminders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('invoice_id')->constrained()->cascadeOnDelete();

            $table->enum('reminder_type', [
                'before_due',    // sent X days before due date
                'on_due',        // sent on due date
                'overdue_1',     // 1 day overdue
                'overdue_7',     // 7 days overdue
                'overdue_14',    // 14 days overdue
                'manual'         // manually triggered by user
            ]);

            $table->timestamp('sent_at');
            $table->boolean('success')->default(true);
            $table->text('error_message')->nullable(); // if sending failed

            $table->timestamps();

            $table->index('invoice_id');
            $table->index(['invoice_id', 'reminder_type']); // prevent duplicate sends
        });
    }

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