<?php

use App\Models\Order;
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('transactions', function (Blueprint $table) {
            $table->id();
            $table->foreignIdFor(Order::class)->constrained('orders')->cascadeOnDelete();
            $table->unsignedBigInteger('parent_transaction_id')->nullable();
            $table->boolean('success')->index();
            $table->string('type')->index()->default('capture');
            $table->string('method');
            $table->unsignedBigInteger('amount')->index();
            $table->string('reference')->index();
            $table->string('status');
            $table->string('notes')->nullable();
            $table->string('card_type')->index();
            $table->string('last_four')->nullable();
            $table->json('meta')->nullable();
            $table->dateTime('captured_at');
            $table->timestamps();
        });
    }

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