-- 邀请码管理 — 菜单 + 权限
WITH system_menu AS (
    SELECT id FROM sys_menu WHERE name = '系统管理' AND type = 1 AND status = '0' LIMIT 1
),
inserted AS (
    INSERT INTO sys_menu (name, type, "order", permission, icon, route_name, route_path, component_path,
                          redirect, hidden, keep_alive, always_show, affix, title, params,
                          uuid, parent_id, status, created_time, updated_time)
    SELECT '邀请码管理', 2, 98,
           'module_system:invitation:list',
           'key', 'Invitation', '/invitation', 'views/module_system/invitation/index.vue',
           null, false, true, false, false, '邀请码管理', null,
           gen_random_uuid(),
           system_menu.id, '0', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
    FROM system_menu
    WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE name = '邀请码管理' AND status = '0')
    RETURNING id
)
INSERT INTO sys_role_menus (role_id, menu_id)
SELECT 1, id FROM inserted;
