-- 根据 Sidebar.tsx 菜单项生成的 sys_routes 表插入语句
-- 智慧法务系统路由权限数据

-- 清理现有数据（可选）
-- DELETE FROM sys_routes WHERE id > 0;
-- ALTER SEQUENCE sys_route_id_seq RESTART WITH 1;

-- 插入一级菜单项
INSERT INTO sys_routes (path, name, meta, parent_id, is_menu) VALUES 
-- 系统概览
('/home', 'home', '{"title": "系统概览", "icon": "ri-home-line", "order": 1}', 0, 1),

-- AI对话
('/chat-with-llm', 'chat-with-llm', '{"title": "AI对话", "icon": "ri-chat-smile-2-line", "order": 2}', 0, 1),

-- 文件管理（父菜单）
('/files', 'file-management', '{"title": "文件管理", "icon": "ri-folder-line", "order": 3}', 0, 1),

-- 评查规则库（父菜单）
('/rules', 'rule-management', '{"title": "评查规则库", "icon": "ri-book-3-line", "order": 4}', 0, 1),

-- 合同模板（父菜单）
('/contract-template', 'contract-template', '{"title": "合同模板", "icon": "ri-file-search-line", "order": 5}', 0, 1),

-- 系统设置（父菜单，需要developer权限）
('/settings', 'system-settings', '{"title": "系统设置", "icon": "ri-settings-4-line", "order": 6, "requiredRole": "developer"}', 0, 1);

-- 交叉评查（父菜单）
('/cross-checking', 'cross-checking', '{"title": "交叉评查", "icon": "ri-color-filter-line", "order": 7}', 0, 1);

-- 插入文件管理的子菜单项
INSERT INTO sys_routes (path, name, meta, parent_id, is_menu) VALUES 
-- 文件上传（文件管理的子菜单，parent_id需要根据实际的file-management记录ID调整）
('/files/upload', 'file-upload', '{"title": "文件上传", "icon": "ri-upload-cloud-line", "order": 1}', 
    (SELECT id FROM sys_routes WHERE name = 'file-management'), 1),

-- 文档列表（文件管理的子菜单）
('/documents', 'documents', '{"title": "文档列表", "icon": "ri-file-list-3-line", "order": 2}', 
    (SELECT id FROM sys_routes WHERE name = 'file-management'), 1);

-- 插入评查规则库的子菜单项
INSERT INTO sys_routes (path, name, meta, parent_id, is_menu) VALUES 
-- 评查点分组（评查规则库的子菜单）
('/rule-groups', 'rule-groups', '{"title": "评查点分组", "icon": "ri-folder-open-line", "order": 1}', 
    (SELECT id FROM sys_routes WHERE name = 'rule-management'), 1),

-- 评查点列表（评查规则库的子菜单）
('/rules', 'rules-list', '{"title": "评查点列表", "icon": "ri-list-check-3", "order": 2}', 
    (SELECT id FROM sys_routes WHERE name = 'rule-management'), 1),

-- 评查文件列表（评查规则库的子菜单）
('/rules-files', 'rules-file', '{"title": "评查文件列表", "icon": "ri-list-check-2", "order": 3}', 
    (SELECT id FROM sys_routes WHERE name = 'rule-management'), 1),

-- 插入合同模板的子菜单项
INSERT INTO sys_routes (path, name, meta, parent_id, is_menu) VALUES 
-- 智能搜索（合同模板的子菜单）
('/contract-template/search', 'contract-search-ai', '{"title": "智能搜索", "icon": "ri-search-line", "order": 1}', 
    (SELECT id FROM sys_routes WHERE name = 'contract-template'), 1),

-- 合同列表（合同模板的子菜单）
('/contract-template/list', 'contract-list', '{"title": "合同列表", "icon": "ri-folder-line", "order": 2}', 
    (SELECT id FROM sys_routes WHERE name = 'contract-template'), 1);

-- 插入系统设置的子菜单项（需要developer权限）
INSERT INTO sys_routes (path, name, meta, parent_id, is_menu) VALUES 
-- 配置列表（系统设置的子菜单，需要developer权限）
('/config-lists', 'config-lists', '{"title": "配置列表", "icon": "ri-list-check-3", "order": 1, "requiredRole": "developer"}', 
    (SELECT id FROM sys_routes WHERE name = 'system-settings'), 1),

-- 文档类型（系统设置的子菜单，需要developer权限）
('/document-types', 'document-types', '{"title": "文档类型", "icon": "ri-file-list-line", "order": 2, "requiredRole": "developer"}', 
    (SELECT id FROM sys_routes WHERE name = 'system-settings'), 1),

-- 提示词管理（系统设置的子菜单，需要developer权限）
('/prompts', 'prompt-management', '{"title": "提示词管理", "icon": "ri-chat-1-line", "order": 3, "requiredRole": "developer"}', 
    (SELECT id FROM sys_routes WHERE name = 'system-settings'), 1);

-- 查询插入结果
SELECT 
    r1.id,
    r1.path,
    r1.name,
    r1.meta,
    r1.parent_id,
    r2.name as parent_name,
    r1.is_menu
FROM sys_routes r1
LEFT JOIN sys_routes r2 ON r1.parent_id = r2.id
ORDER BY 
    CASE WHEN r1.parent_id = 0 THEN r1.id ELSE r1.parent_id END,
    r1.parent_id,
    (r1.meta->>'order')::int; 