-- 财务专员角色与财务模块权限。
INSERT INTO admin_roles (role_key, role_name, description)
VALUES ('finance', '财务', '负责财务报表、资金申请审核和打款核对')
ON CONFLICT (role_key) DO UPDATE SET
  role_name = EXCLUDED.role_name,
  description = EXCLUDED.description,
  updated_at = NOW();

INSERT INTO admin_permissions (permission_key, permission_name, permission_group, description)
VALUES
  ('finance.view', '查看财务报表', '财务管理', '查看财务总览、日报和资金明细'),
  ('finance.review', '审核资金申请', '财务管理', '审核充值和提现申请'),
  ('finance.config', '配置财务规则', '财务管理', '维护充值、提现和押金规则'),
  ('finance.export', '导出财务数据', '财务管理', '导出筛选后的财务明细')
ON CONFLICT (permission_key) DO UPDATE SET
  permission_name = EXCLUDED.permission_name,
  permission_group = EXCLUDED.permission_group,
  description = EXCLUDED.description,
  updated_at = NOW();

-- 管理员保留全部权限；运营兼容现有资金审核能力；财务专员只获得财务相关权限。
INSERT INTO admin_role_permissions (role_key, permission_key)
SELECT 'admin', permission_key
FROM admin_permissions
WHERE permission_key LIKE 'finance.%'
ON CONFLICT DO NOTHING;

INSERT INTO admin_role_permissions (role_key, permission_key)
VALUES ('operator', 'finance.view'), ('operator', 'finance.review')
ON CONFLICT DO NOTHING;

INSERT INTO admin_role_permissions (role_key, permission_key)
VALUES ('finance', 'finance.view'), ('finance', 'finance.review')
ON CONFLICT DO NOTHING;
