<?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('invoice_transactions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('support_invoice_id')->constrained('support_invoices')->onDelete('cascade');
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            
            // Transaction details
            $table->string('transaction_id')->unique(); // Stripe payment intent ID
            $table->decimal('amount', 10, 2);
            $table->string('currency', 3)->default('usd');
            $table->string('payment_method')->nullable(); // card, bank_transfer, etc.
            $table->enum('status', ['pending', 'succeeded', 'failed'])->default('pending');
            
            // Stripe details
            $table->string('stripe_payment_intent_id')->nullable();
            $table->string('stripe_charge_id')->nullable();
            $table->text('payment_method_details')->nullable(); // JSON
            
            // Timestamps
            $table->timestamp('paid_at')->nullable();
            $table->timestamps();
            
            $table->index('support_invoice_id');
            $table->index('user_id');
            $table->index('transaction_id');
        });
    }

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