<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     * FIX: Add missing columns that PaymentController::create() is trying to use
     */
    public function up(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            // Add shipping info columns
            if (!Schema::hasColumn('orders', 'shipping_country')) {
                if (!Schema::hasColumn('orders', 'shipping_country')) { $table->string('shipping_country')->nullable(); }
            }
            
            if (!Schema::hasColumn('orders', 'shipping_district')) {
                if (!Schema::hasColumn('orders', 'shipping_district')) { $table->string('shipping_district')->nullable(); }
            }
            
            if (!Schema::hasColumn('orders', 'shipping_fee')) {
                if (!Schema::hasColumn('orders', 'shipping_fee')) { $table->decimal('shipping_fee', 15, 2)->default(0); }
            }
            
            if (!Schema::hasColumn('orders', 'discount_percentage')) {
                if (!Schema::hasColumn('orders', 'discount_percentage')) { $table->decimal('discount_percentage', 5, 2)->default(0); }
            }
            
            if (!Schema::hasColumn('orders', 'payment_gateway_response')) {
                if (!Schema::hasColumn('orders', 'payment_gateway_response')) { $table->json('payment_gateway_response')->nullable(); }
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $columns = ['shipping_country', 'shipping_district', 'shipping_fee', 'discount_percentage', 'payment_gateway_response'];
            foreach ($columns as $column) {
                if (Schema::hasColumn('orders', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};


