<?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('orders')) {
            return;
        }

        Schema::table('orders', function (Blueprint $table) {
            if (!Schema::hasColumn('orders', 'use_ppn')) {
                $table->boolean('use_ppn')->default(false)->after('total_price');
            }

            if (!Schema::hasColumn('orders', 'ppn_rate')) {
                $table->decimal('ppn_rate', 5, 2)->nullable()->after('use_ppn');
            }

            if (!Schema::hasColumn('orders', 'ppn_amount')) {
                $table->decimal('ppn_amount', 15, 2)->default(0)->after('ppn_rate');
            }

            if (!Schema::hasColumn('orders', 'subtotal_before_ppn')) {
                $table->decimal('subtotal_before_ppn', 15, 2)->nullable()->after('ppn_amount');
            }
        });
    }

    public function down(): void
    {
        if (!Schema::hasTable('orders')) {
            return;
        }

        Schema::table('orders', function (Blueprint $table) {
            foreach (['subtotal_before_ppn', 'ppn_amount', 'ppn_rate', 'use_ppn'] as $column) {
                if (Schema::hasColumn('orders', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
