<?php

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

/**
 * Adds the mega_menu columns to site_menu.
 *
 * When mega_menu is enabled, the menu item's children are hand-picked from
 * mega_menu_items — an ordered JSON list of category/product refs such as
 * [{"type":"category","id":12},{"type":"product","id":345}] — rather than being
 * generated from a single source category (category_list / product_list).
 * Idempotent so it is safe on installs that may already have run it.
 */
return new class extends Migration
{
    public function up(): void
    {
        if (!Schema::hasTable('site_menu')) {
            return;
        }

        Schema::table('site_menu', function (Blueprint $table) {
            if (!Schema::hasColumn('site_menu', 'mega_menu')) {
                $table->tinyInteger('mega_menu')->default(0)->after('product_list');
            }
            if (!Schema::hasColumn('site_menu', 'mega_menu_items')) {
                $table->text('mega_menu_items')->nullable()->after('mega_menu');
            }
        });
    }

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

        Schema::table('site_menu', function (Blueprint $table) {
            if (Schema::hasColumn('site_menu', 'mega_menu_items')) {
                $table->dropColumn('mega_menu_items');
            }
            if (Schema::hasColumn('site_menu', 'mega_menu')) {
                $table->dropColumn('mega_menu');
            }
        });
    }
};
