-- ============================================
-- 初始数据脚本
-- 包含权限、角色、系统配置等基础数据
-- ============================================

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 密码 hash: admin123456
INSERT INTO admin_users (username, password_hash, nickname, status) VALUES
('admin', '$2a$10$vvDd.cYJCOBl9kr5jdRYXu9C/7GXcHmXsG17zJPHzPOZ0XGbqcWNS', '超级管理员', 'active'),
('kf1', '$2a$10$7cx3B4IWunLZO24o11YVGu4qY1J1naDdsTyvoCpcnOO.GV15qaf3C', '客服1', 'active'),
('kf2', '$2a$10$m6kLCXciFygpqIGUZ9LQtuEFAl40hb.S7zJmMiuC83WrU7OQM3TPC', '客服2', 'active')
ON DUPLICATE KEY UPDATE nickname = VALUES(nickname);

-- 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';

-- 客服关联 cs 角色
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 IN ('kf1', 'kf2') AND r.code = 'cs';

-- -------------------------------------------
-- 系统配置
-- -------------------------------------------

INSERT INTO system_configs (`key`, `value`, description) VALUES
('order.handoff_timeout_minutes', '60', '交接超时时间（分钟）'),
('order.return_timeout_minutes', '1440', '归还超时时间（分钟）'),
('sms.rate_limit_per_minute', '1', '短信限流：每分钟最多发送次数'),
('sms.rate_limit_per_hour', '5', '短信限流：每小时最多发送次数'),
('wallet.min_deposit', '50', '最低押金金额'),
('platform.fee_ratio', '0.1', '平台抽成比例'),
('listing.review_required', 'true', '商品是否需要审核'),
('order.pending_payment_timeout_minutes', '15', '待支付超时时间（分钟）'),
('chat.default_support_admin_id', '1', '订单群聊回退客服 ID（仅当无可用客服时使用）'),
('chat.auto_welcome_message', '欢迎加入订单群聊！如有任何问题，请随时沟通。', '建群后自动发送的欢迎话术')
ON DUPLICATE KEY UPDATE description = VALUES(description);
