<?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::table('payments', function (Blueprint $table) {
            $table->foreignId('subscription_id')->nullable()->after('user_id')->constrained()->nullOnDelete();
            $table->foreignId('plan_id')->nullable()->after('subscription_id')->constrained()->nullOnDelete();
            $table->string('currency', 3)->default('USD')->after('amount');
            $table->string('gateway_payment_id')->nullable()->after('transaction_id');
            $table->string('gateway_customer_id')->nullable()->after('gateway_payment_id');
            $table->json('metadata')->nullable()->after('status');
            $table->timestamp('paid_at')->nullable()->after('metadata');

            $table->index('gateway_payment_id');
            $table->index('status');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('payments', function (Blueprint $table) {
            $table->dropForeign(['subscription_id']);
            $table->dropForeign(['plan_id']);
            $table->dropColumn([
                'subscription_id',
                'plan_id',
                'currency',
                'gateway_payment_id',
                'gateway_customer_id',
                'metadata',
                'paid_at',
            ]);
        });
    }
};
