<?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
    {
        if (Schema::hasTable('orders')) {
            Schema::table('orders', function (Blueprint $table) {
                if (!Schema::hasColumn('orders', 'transaction_id')) {
                    $table->string('transaction_id')->nullable()->after('payment_status');
                }
                if (!Schema::hasColumn('orders', 'phonepe_reference_id')) {
                    $table->string('phonepe_reference_id')->nullable()->after('transaction_id');
                }
                if (!Schema::hasColumn('orders', 'payment_details')) {
                    $table->text('payment_details')->nullable()->after('phonepe_reference_id');
                }
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        if (Schema::hasTable('orders')) {
            Schema::table('orders', function (Blueprint $table) {
                $columns = [];
                if (Schema::hasColumn('orders', 'transaction_id')) $columns[] = 'transaction_id';
                if (Schema::hasColumn('orders', 'phonepe_reference_id')) $columns[] = 'phonepe_reference_id';
                if (Schema::hasColumn('orders', 'payment_details')) $columns[] = 'payment_details';
                if (!empty($columns)) {
                    $table->dropColumn($columns);
                }
            });
        }
    }
};
