-- Add System Settings menu to platform management
-- This should be executed after the system is running

-- Insert Platform Settings menu under Platform Management (assuming parent_id=4 for Platform Management)
INSERT INTO menus (name, title, path, icon, parent_id, sort_order, is_active, created_at, updated_at)
VALUES (
    'system_settings',
    '系统参数',
    '/admin/system-settings',
    'settings',
    (SELECT id FROM menus WHERE name = 'platform_management'),
    1,
    true,
    NOW(),
    NOW()
)
ON CONFLICT (name) DO UPDATE
SET
    title = EXCLUDED.title,
    path = EXCLUDED.path,
    icon = EXCLUDED.icon,
    parent_id = EXCLUDED.parent_id,
    sort_order = EXCLUDED.sort_order,
    updated_at = NOW();

-- Grant access to admin role
INSERT INTO role_menus (role_id, menu_id)
SELECT
    r.id,
    m.id
FROM
    roles r,
    menus m
WHERE
    r.name = 'admin'
    AND m.name = 'system_settings'
ON CONFLICT (role_id, menu_id) DO NOTHING;

-- Verify the menu was added
SELECT
    m.id,
    m.name,
    m.title,
    m.path,
    m.icon,
    parent.title as parent_menu,
    m.sort_order
FROM menus m
LEFT JOIN menus parent ON m.parent_id = parent.id
WHERE m.name = 'system_settings';
