<?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
    {
        if (! Schema::hasTable('payments')) {
            Schema::create('payments', function (Blueprint $table): void {
                $table->id();
                $table->foreignId('account_id')->constrained()->cascadeOnDelete();
                $table->bigInteger('amount_cents');
                $table->string('currency', 3)->default('EUR');
                $table->string('reference')->nullable();
                $table->timestamp('paid_at')->nullable();
                $table->jsonb('meta')->nullable();
                $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
                $table->timestamps();
                $table->softDeletes();

                $table->index(['account_id', 'paid_at']);
                $table->index('reference');
            });
        }
    }

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