<?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
    {
        Schema::create('provider_transactions', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('transaction_id')->constrained()->cascadeOnDelete();
            $table->string('provider')->default('vtpass');
            $table->string('provider_transaction_id')->nullable();
            $table->string('request_id')->nullable();
            $table->json('request_payload')->nullable();
            $table->json('response_payload')->nullable();
            $table->decimal('amount', 15, 2);
            $table->enum('status', ['pending', 'processing', 'successful', 'failed'])->default('pending');
            $table->text('error_message')->nullable();
            $table->integer('attempt_count')->default(0);
            $table->timestamp('last_attempted_at')->nullable();
            $table->timestamps();

            $table->index(['transaction_id']);
            $table->index('provider_transaction_id');
            $table->index('request_id');
            $table->index('status');
        });
    }

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