<?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: Make 'total' column nullable to match PaymentController::create() which uses 'total_amount' instead
     */
    public function up(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            // Make 'total' column nullable since PaymentController uses 'total_amount'
            // This prevents "Field 'total' doesn't have a default value" errors
            if (Schema::hasColumn('orders', 'total')) {
                try {
                    if (!Schema::hasColumn('orders', 'total')) { $table->decimal('total', 15, 2)->nullable()->change(); }
                } catch (\Exception $e) {
                    // Already nullable or other issue
                }
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            // Don't revert on rollback - just leave it nullable
            // as it's safer for data integrity
        });
    }
};

