<?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
    {
        if (! Schema::hasTable('bookings')) {
            Schema::create('bookings', function (Blueprint $table): void {
                $table->id();
                $table->foreignId('account_id')->constrained()->cascadeOnDelete();
                $table->foreignId('payment_id')->nullable()->constrained('payments')->nullOnDelete();
                $table->bigInteger('amount_cents');
                $table->enum('type', ['debit', 'credit']);
                $table->string('description')->nullable();
                $table->timestamp('booked_at')->nullable();
                $table->timestamps();
                $table->softDeletes();

                $table->index(['account_id', 'booked_at']);
            });
        }
    }

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