<?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('payment_transactions', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('investment_id')->constrained('investments')->cascadeOnDelete();
            $table->string('paystack_reference')->unique();
            $table->string('paystack_transaction_id')->nullable();
            $table->decimal('amount', 12, 2);
            $table->string('currency', 3)->default('USD');
            $table->enum('status', ['pending', 'success', 'failed', 'abandoned', 'refunded'])->default('pending');
            $table->string('channel')->nullable()->comment('card, bank, ussd, etc.');
            $table->string('card_type')->nullable();
            $table->string('card_last4', 4)->nullable();
            $table->string('bank_name')->nullable();
            $table->string('authorization_code')->nullable();
            $table->json('paystack_response')->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->timestamp('paid_at')->nullable();
            $table->timestamps();
        });

        Schema::table('payment_transactions', function (Blueprint $table) {
            $table->index('paystack_reference', 'idx_paystack_reference');
            $table->index('status', 'idx_transaction_status');
        });
    }

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