<?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
    {
        $columns = [
            'payment_status' => fn (Blueprint $table) => $table->string('payment_status')->default('pending')->after('status'),
            'order_status' => fn (Blueprint $table) => $table->string('order_status')->default('pending')->after('payment_status'),
            'transaction_id' => fn (Blueprint $table) => $table->string('transaction_id')->nullable()->after('note'),
            'approval_code' => fn (Blueprint $table) => $table->string('approval_code')->nullable()->after('transaction_id'),
            'paid_at' => fn (Blueprint $table) => $table->timestamp('paid_at')->nullable()->after('approval_code'),
            'cancelled_at' => fn (Blueprint $table) => $table->timestamp('cancelled_at')->nullable()->after('paid_at'),
            'completed_at' => fn (Blueprint $table) => $table->timestamp('completed_at')->nullable()->after('cancelled_at'),
        ];

        foreach ($columns as $column => $definition) {
            if (! Schema::hasColumn('orders', $column)) {
                Schema::table('orders', $definition);
            }
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // These columns are part of the canonical orders table on fresh installs.
        // Keeping them during rollback avoids removing columns owned by the create migration.
    }
};
