-- ============================================
-- 权限系统初始化数据
-- 包含：用户 - 角色关联、角色 - 权限关联、菜单数据
-- ============================================

-- 1. 初始化用户 - 角色关联
-- admin 用户 (id=1) 关联 super_admin 角色 (id=1)
INSERT INTO t_auth_user_role (user_id, role_id) VALUES
(1, 1)  -- admin 用户 -> super_admin 角色
ON DUPLICATE KEY UPDATE `user_id` = VALUES(`user_id`);

-- 2. 初始化角色 - 权限关联
-- super_admin 角色 (id=1) 关联所有权限
INSERT INTO t_auth_role_permission (role_id, permission_id) VALUES
(1, 1),   -- user:create
(1, 2),   -- user:read
(1, 3),   -- user:update
(1, 4),   -- user:delete
(1, 5),   -- user:manage
(1, 6),   -- role:create
(1, 7),   -- role:read
(1, 8),   -- role:update
(1, 9),   -- role:delete
(1, 10),  -- tenant:create
(1, 11),  -- tenant:read
(1, 12),  -- tenant:update
(1, 13),  -- tenant:delete
(1, 14)   -- super_admin
ON DUPLICATE KEY UPDATE `role_id` = VALUES(`role_id`);

-- admin 角色 (id=2) 关联基础业务权限（不含租户管理和超管权限）
INSERT INTO t_auth_role_permission (role_id, permission_id) VALUES
(2, 1),   -- user:create
(2, 2),   -- user:read
(2, 3),   -- user:update
(2, 4),   -- user:delete
(2, 5),   -- user:manage
(2, 6),   -- role:create
(2, 7),   -- role:read
(2, 8),   -- role:update
(2, 9)    -- role:delete
ON DUPLICATE KEY UPDATE `role_id` = VALUES(`role_id`);

-- user 角色 (id=3) 关联只读权限
INSERT INTO t_auth_role_permission (role_id, permission_id) VALUES
(3, 2),   -- user:read
(3, 7)    -- role:read
ON DUPLICATE KEY UPDATE `role_id` = VALUES(`role_id`);

-- 3. 初始化菜单数据
-- ============================================
-- 一级菜单：工作台 (id=1)
-- ============================================
INSERT INTO t_auth_menu (id, name, path, icon, component, parent_id, permission, sort, valid_state) VALUES
(1, '工作台', 'workbench', 'Dashboard', 'workbench/Index', 0, NULL, 1, 1)
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);

-- ============================================
-- 一级菜单：活动管理 (id=2)
-- ============================================
INSERT INTO t_auth_menu (id, name, path, icon, component, parent_id, permission, sort, valid_state) VALUES
(2, '活动管理', 'contests', 'Trophy', NULL, 0, NULL, 2, 1)
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);

-- 活动管理子菜单
INSERT INTO t_auth_menu (name, path, icon, component, parent_id, permission, sort, valid_state) VALUES
('活动列表', '', 'Appstore', 'contests/Index', 2, 'contest:read', 1, 1),
('创建活动', 'create', 'Plus', 'contests/Create', 2, 'contest:create', 2, 1),
('报名管理', 'registrations', 'Team', 'contests/registrations/Index', 2, 'contest:registration', 3, 1),
('作品管理', 'works', 'FileText', 'contests/works/Index', 2, 'contest:work', 4, 1),
('评审管理', 'reviews', 'CheckSquare', 'contests/reviews/Index', 2, 'contest:review', 5, 1),
('评委管理', 'judges', 'User', 'contests/judges/Index', 2, 'contest:judge', 6, 1),
('评审结果', 'results', 'BarChart', 'contests/results/Index', 2, 'contest:result', 7, 1),
('评审规则', 'rules', 'Setting', 'contests/ReviewRules', 2, 'contest:rule', 8, 1),
('公告管理', 'notices', 'Bell', 'contests/notices/Index', 2, 'contest:notice', 9, 1)
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);

-- ============================================
-- 一级菜单：学校管理 (id=12)
-- ============================================
INSERT INTO t_auth_menu (id, name, path, icon, component, parent_id, permission, sort, valid_state) VALUES
(12, '学校管理', 'school', 'School', NULL, 0, NULL, 3, 1)
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);

-- 学校管理子菜单
INSERT INTO t_auth_menu (name, path, icon, component, parent_id, permission, sort, valid_state) VALUES
('学校信息', '', 'Home', 'school/schools/Index', 12, 'school:read', 1, 1),
('部门管理', 'departments', 'Building', 'school/departments/Index', 12, 'department:read', 2, 1),
('年级管理', 'grades', 'Layer', 'school/grades/Index', 12, 'grade:read', 3, 1),
('班级管理', 'classes', 'Team', 'school/classes/Index', 12, 'class:read', 4, 1),
('教师管理', 'teachers', 'User', 'school/teachers/Index', 12, 'teacher:read', 5, 1),
('学生管理', 'students', 'Team', 'school/students/Index', 12, 'student:read', 6, 1)
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);

-- ============================================
-- 一级菜单：系统管理 (id=19) - 仅 super_admin 可见
-- ============================================
INSERT INTO t_auth_menu (id, name, path, icon, component, parent_id, permission, sort, valid_state) VALUES
(19, '系统管理', 'system', 'Setting', NULL, 0, 'super_admin', 4, 1)
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);

-- 系统管理子菜单
INSERT INTO t_auth_menu (name, path, icon, component, parent_id, permission, sort, valid_state) VALUES
('用户管理', 'users', 'User', 'system/users/Index', 19, 'user:read', 1, 1),
('角色管理', 'roles', 'Team', 'system/roles/Index', 19, 'role:read', 2, 1),
('权限管理', 'permissions', 'Lock', 'system/permissions/Index', 19, 'permission:read', 3, 1),
('菜单管理', 'menus', 'Menu', 'system/menus/Index', 19, 'menu:read', 4, 1),
('租户管理', 'tenants', 'Cluster', 'system/tenants/Index', 19, 'tenant:read', 5, 1),
('字典管理', 'dict', 'Book', 'system/dict/Index', 19, 'dict:read', 6, 1),
('配置管理', 'config', 'Setting', 'system/config/Index', 19, 'config:read', 7, 1),
('日志管理', 'logs', 'FileText', 'system/logs/Index', 19, 'log:read', 8, 1)
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);

-- 4. 初始化租户 - 菜单关联（租户 ID=1 关联所有菜单）
-- 获取所有菜单 ID 并关联到租户 1
INSERT INTO t_auth_tenant_menu (tenant_id, menu_id)
SELECT 1, id FROM t_auth_menu WHERE deleted = 0
ON DUPLICATE KEY UPDATE `tenant_id` = VALUES(`tenant_id`);
