-- 1. 确保 Admin 角色存在
INSERT IGNORE INTO `roles` (`id`, `name`, `description`) VALUES (1, 'admin', '系统管理员');

-- 2. 确保 lq 用户关联到 Admin 角色
UPDATE `users` SET `role_id` = 1 WHERE `username` = 'lq';

-- 3. 确保权限表中有数据 (如果为空)
INSERT IGNORE INTO `permissions` (`name`, `resource`, `action`) VALUES 
('Create User', 'users', 'create'),
('Read User', 'users', 'read'),
('Update User', 'users', 'update'),
('Delete User', 'users', 'delete'),
('Read Dashboard', 'dashboard', 'read'),
('Create Post', 'posts', 'create'),
('Read Post', 'posts', 'read'),
('Update Post', 'posts', 'update'),
('Delete Post', 'posts', 'delete'),
('Create Role', 'roles', 'create'),
('Read Role', 'roles', 'read'),
('Update Role', 'roles', 'update'),
('Delete Role', 'roles', 'delete');

-- 4. 清空旧的关联 (为了安全起见，防止重复)
DELETE FROM `role_permissions` WHERE role_id = 1;

-- 5. 重新赋予 Admin 所有权限
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT 1, id FROM `permissions`;

SELECT 'Permissions fixed successfully' as result;
