<?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('payments', function (Blueprint $table) {
            $table->uuid('id')->primary();

            $table->foreignUuid('user_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignUuid('subscription_id')->nullable()->constrained()->nullOnDelete();

            $table->integer('amount');
            $table->string('currency')->default('EUR');

            $table->string('status'); // paid, failed, pending

            $table->string('provider');
            $table->string('provider_payment_id')->nullable();

            $table->string('invoice_id')->nullable();
            $table->string('billing_reason')->nullable();
            $table->unsignedInteger('attempt_count')->default(0);

            $table->timestamp('paid_at')->nullable();

            $table->index('invoice_id');

            $table->unique(['provider', 'provider_payment_id'], 'payments_provider_payment_unique');

            $table->timestamps();
        });
    }

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