-- RBAC seed: 默认角色和权限
SET NAMES utf8mb4;

-- 插入权限
INSERT INTO permissions (code, name, resource, action) VALUES
('dashboard:view',        '查看仪表盘',       'dashboard',      'view'),
('user:view',             '查看用户',         'user',           'view'),
('user:freeze',           '冻结用户',         'user',           'freeze'),
('user:unfreeze',         '解冻用户',         'user',           'unfreeze'),
('order:view',            '查看订单',         'order',          'view'),
('order:close',           '关闭订单',         'order',          'close'),
('order:mark_abnormal',   '标记异常订单',     'order',          'mark_abnormal'),
('listing:view',          '查看商品',         'listing',        'view'),
('listing:approve',       '审核通过商品',     'listing',        'approve'),
('listing:reject',        '审核拒绝商品',     'listing',        'reject'),
('listing:offline',       '下架商品',         'listing',        'offline'),
('dispute:view',          '查看纠纷',         'dispute',        'view'),
('dispute:arbitrate',     '仲裁纠纷',         'dispute',        'arbitrate'),
('chat:view',             '查看客服群聊',     'chat',           'view'),
('chat:send',             '发送客服消息',     'chat',           'send'),
('wallet:view',           '查看资金流水',     'wallet',         'view'),
('admin_user:manage',     '管理员管理',       'admin_user',     'manage'),
('role:manage',           '角色管理',         'role',           'manage'),
('system_config:view',    '查看系统配置',     'system_config',  'view'),
('system_config:update',  '修改系统配置',     'system_config',  'update'),
('audit_log:view',        '查看审计日志',     'audit_log',      'view')
ON DUPLICATE KEY UPDATE name = VALUES(name);

-- 插入角色
INSERT INTO roles (code, name, description) VALUES
('super_admin', '超级管理员', '拥有所有权限，不可被删除'),
('cs',          '客服',       '处理用户咨询、群聊、纠纷仲裁'),
('ops',         '运营',       '管理商品审核、订单管理、用户管理'),
('finance',     '财务',       '查看资金流水、订单信息')
ON DUPLICATE KEY UPDATE name = VALUES(name), description = VALUES(description);

-- super_admin 拥有所有权限
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r CROSS JOIN permissions p WHERE r.code = 'super_admin';

-- cs 角色权限
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r, permissions p
WHERE r.code = 'cs' AND p.code IN ('dashboard:view', 'user:view', 'dispute:view', 'dispute:arbitrate', 'chat:view', 'chat:send', 'order:view');

-- ops 角色权限
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r, permissions p
WHERE r.code = 'ops' AND p.code IN ('dashboard:view', 'user:view', 'user:freeze', 'user:unfreeze', 'order:view', 'order:close', 'order:mark_abnormal', 'listing:view', 'listing:approve', 'listing:reject', 'listing:offline', 'dispute:view', 'audit_log:view');

-- finance 角色权限
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r, permissions p
WHERE r.code = 'finance' AND p.code IN ('dashboard:view', 'wallet:view', 'order:view');

-- 默认 admin 用户自动绑定 super_admin 角色
INSERT IGNORE INTO admin_user_roles (admin_user_id, role_id)
SELECT au.id, r.id FROM admin_users au, roles r WHERE au.username = 'admin' AND r.code = 'super_admin';
