<?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('invoices', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->string('invoice_number')->unique()->comment('Unique invoice identifier');
            $table->decimal('amount', 10, 2)->comment('Invoice amount');
            $table->string('currency', 3)->default('usd')->comment('Currency code (ISO 4217)');
            $table->enum('status', ['draft', 'pending', 'paid', 'failed', 'cancelled'])->default('pending');
            $table->date('due_date')->nullable()->comment('Payment due date');
            $table->timestamp('paid_at')->nullable()->comment('Date when invoice was paid');
            $table->json('metadata')->nullable()->comment('Additional invoice metadata');
            $table->timestamps();

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

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