<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        $productCatalog = DB::table('menus')->where('href', '/admin/products')->first();
        
        $sortOrder = $productCatalog ? $productCatalog->sort_order + 1 : 7;
        
        DB::table('menus')->where('sort_order', '>=', $sortOrder)->increment('sort_order');
        
        DB::table('menus')->insert([
            'group_name' => 'CafePOS Management',
            'title' => 'Add-ons Manager',
            'href' => '/admin/modifiers',
            'icon' => 'PlusSquare', // A suitable icon name from lucide-vue-next
            'roles' => json_encode([]),
            'permissions' => json_encode(['products.manage']),
            'sort_order' => $sortOrder,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]);
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        $menu = DB::table('menus')->where('href', '/admin/modifiers')->first();
        if ($menu) {
            $sortOrder = $menu->sort_order;
            DB::table('menus')->where('id', $menu->id)->delete();
            DB::table('menus')->where('sort_order', '>', $sortOrder)->decrement('sort_order');
        }
    }
};
