<?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
    {
        Schema::table('vouchers', function (Blueprint $table) {
            if (!Schema::hasColumn('vouchers', 'type')) {
                // 'plan' = grants a plan, 'percent' = discount %, 'fixed' = flat discount
                $table->string('type')->nullable()->default('plan')->after('code');
            }
            if (!Schema::hasColumn('vouchers', 'value')) {
                // Discount/credit amount for percent/fixed vouchers; null for plan vouchers
                $table->decimal('value', 14, 4)->nullable()->after('type');
            }
        });
    }

    public function down(): void
    {
        Schema::table('vouchers', function (Blueprint $table) {
            $table->dropColumn(array_filter(['type', 'value'], function ($col) {
                return Schema::hasColumn('vouchers', $col);
            }));
        });
    }
};
