/**
 * 角色权限类型定义
 * 对应后端数据库中的角色和权限
 */

// 角色类型 - 对应 sys_role 表
export type AdminRole =
  | 'super_admin'       // 超级管理员
  | 'system_admin'      // 系统管理员
  | 'operation_manager' // 运营经理
  | 'operation_member'  // 运营成员
  | 'marketing_manager' // 市场经理
  | 'marketing_member'  // 市场成员
  | 'finance_manager'   // 财务经理
  | 'finance_member'    // 财务成员
  | 'risk_manager'      // 风控经理
  | 'risk_member'       // 风控成员
  | 'customer_service'  // 客服
  | 'auditor'           // 审计员
  | 'viewer'            // 只读

// 权限代码 - 对应 sys_permission 表
export type Permission =
  // 仪表盘
  | 'dashboard:view'
  | 'dashboard:export'

  // 用户管理
  | 'user:view'
  | 'user:create'
  | 'user:update'
  | 'user:delete'
  | 'user:freeze'
  | 'user:unfreeze'
  | 'user:certify'
  | 'user:export'

  // 活动管理
  | 'activity:view'
  | 'activity:create'
  | 'activity:update'
  | 'activity:delete'
  | 'activity:publish'
  | 'activity:pause'
  | 'activity:end'
  | 'activity:export'

  // 奖励管理
  | 'reward:view'
  | 'reward:approve'
  | 'reward:发放'
  | 'reward:reject'
  | 'reward:export'

  // 风险管理
  | 'risk:view'
  | 'risk:rule'
  | 'risk:audit'
  | 'risk:blacklist'
  | 'risk:export'

  // 审批中心
  | 'approval:view'
  | 'approval:handle'
  | 'approval:delegate'

  // 审计日志
  | 'audit:view'
  | 'audit:export'

  // 系统配置
  | 'system:view'
  | 'system:config'
  | 'system:cache'

  // 权限管理
  | 'permission:view'
  | 'permission:manage'
  | 'role:view'
  | 'role:manage'
  | 'dept:view'
  | 'dept:manage'

// 数据权限范围
export type DataScope = 'ALL' | 'DEPARTMENT' | 'OWN'

// 角色信息
export interface RoleInfo {
  id?: number
  roleCode: string
  roleName: string
  roleLevel: number
  dataScope: DataScope
  description?: string
  status: number
  createdAt?: string
}

// 权限信息
export interface PermissionInfo {
  id?: number
  permissionCode: string
  permissionName: string
  moduleCode: string
  resourceCode: string
  operationCode: string
  dataScope: DataScope
  description?: string
}

// 角色权限映射
export const RolePermissions: Record<AdminRole, Permission[]> = {
  super_admin: [
    'dashboard:view', 'dashboard:export',
    'user:view', 'user:create', 'user:update', 'user:delete', 'user:freeze', 'user:unfreeze', 'user:certify', 'user:export',
    'activity:view', 'activity:create', 'activity:update', 'activity:delete', 'activity:publish', 'activity:pause', 'activity:end', 'activity:export',
    'reward:view', 'reward:approve', 'reward:发放', 'reward:reject', 'reward:export',
    'risk:view', 'risk:rule', 'risk:audit', 'risk:blacklist', 'risk:export',
    'approval:view', 'approval:handle', 'approval:delegate',
    'audit:view', 'audit:export',
    'system:view', 'system:config', 'system:cache',
    'permission:view', 'permission:manage', 'role:view', 'role:manage', 'dept:view', 'dept:manage'
  ],
  system_admin: [
    'dashboard:view', 'dashboard:export',
    'user:view', 'user:create', 'user:update', 'user:delete', 'user:freeze', 'user:unfreeze', 'user:export',
    'activity:view', 'activity:create', 'activity:update', 'activity:delete', 'activity:export',
    'approval:view', 'approval:handle',
    'audit:view', 'audit:export',
    'system:view', 'system:config', 'system:cache',
    'permission:view', 'role:view', 'dept:view', 'dept:manage'
  ],
  operation_manager: [
    'dashboard:view', 'dashboard:export',
    'user:view', 'user:export',
    'activity:view', 'activity:create', 'activity:update', 'activity:publish', 'activity:pause', 'activity:end', 'activity:export',
    'reward:view', 'reward:approve', 'reward:export',
    'approval:view', 'approval:handle'
  ],
  operation_member: [
    'dashboard:view',
    'activity:view', 'activity:create', 'activity:update',
    'reward:view'
  ],
  marketing_manager: [
    'dashboard:view', 'dashboard:export',
    'user:view', 'user:export',
    'activity:view', 'activity:create', 'activity:update', 'activity:publish', 'activity:export',
    'reward:view', 'reward:approve', 'reward:export',
    'approval:view', 'approval:handle'
  ],
  marketing_member: [
    'dashboard:view',
    'activity:view', 'activity:create', 'activity:update'
  ],
  finance_manager: [
    'dashboard:view', 'dashboard:export',
    'reward:view', 'reward:approve', 'reward:发放', 'reward:export',
    'approval:view', 'approval:handle',
    'audit:view', 'audit:export'
  ],
  finance_member: [
    'dashboard:view',
    'reward:view', 'reward:approve'
  ],
  risk_manager: [
    'dashboard:view', 'dashboard:export',
    'risk:view', 'risk:rule', 'risk:audit', 'risk:blacklist', 'risk:export',
    'user:view', 'user:freeze', 'user:export',
    'approval:view', 'approval:handle',
    'audit:view', 'audit:export'
  ],
  risk_member: [
    'dashboard:view',
    'risk:view', 'risk:audit', 'risk:blacklist'
  ],
  customer_service: [
    'dashboard:view',
    'user:view', 'user:update', 'user:certify',
    'activity:view'
  ],
  auditor: [
    'dashboard:view', 'dashboard:export',
    'user:view', 'user:export',
    'activity:view', 'activity:export',
    'reward:view', 'reward:export',
    'risk:view', 'risk:export',
    'audit:view', 'audit:export',
    'system:view'
  ],
  viewer: [
    'dashboard:view',
    'user:view',
    'activity:view',
    'reward:view',
    'risk:view'
  ]
}

// 兼容旧版角色
export const LegacyRoleMapping: Record<string, AdminRole> = {
  'admin': 'super_admin',
  'operator': 'operation_manager',
  'viewer': 'viewer'
}

// 角色显示名称
export const RoleLabels: Record<AdminRole, string> = {
  super_admin: '超级管理员',
  system_admin: '系统管理员',
  operation_manager: '运营经理',
  operation_member: '运营成员',
  marketing_manager: '市场经理',
  marketing_member: '市场成员',
  finance_manager: '财务经理',
  finance_member: '财务成员',
  risk_manager: '风控经理',
  risk_member: '风控成员',
  customer_service: '客服',
  auditor: '审计员',
  viewer: '只读'
}

// 权限显示名称
export const PermissionLabels: Record<Permission, string> = {
  'dashboard:view': '查看仪表盘',
  'dashboard:export': '导出仪表盘',
  'user:view': '查看用户',
  'user:create': '创建用户',
  'user:update': '更新用户',
  'user:delete': '删除用户',
  'user:freeze': '冻结用户',
  'user:unfreeze': '解冻用户',
  'user:certify': '实名认证',
  'user:export': '导出用户',
  'activity:view': '查看活动',
  'activity:create': '创建活动',
  'activity:update': '更新活动',
  'activity:delete': '删除活动',
  'activity:publish': '发布活动',
  'activity:pause': '暂停活动',
  'activity:end': '结束活动',
  'activity:export': '导出活动',
  'reward:view': '查看奖励',
  'reward:approve': '审批奖励',
  'reward:发放': '发放奖励',
  'reward:reject': '拒绝奖励',
  'reward:export': '导出奖励',
  'risk:view': '查看风控',
  'risk:rule': '管理风控规则',
  'risk:audit': '审核风控',
  'risk:blacklist': '管理黑名单',
  'risk:export': '导出风控',
  'approval:view': '查看审批',
  'approval:handle': '处理审批',
  'approval:delegate': '委托审批',
  'audit:view': '查看审计',
  'audit:export': '导出审计',
  'system:view': '查看系统',
  'system:config': '系统配置',
  'system:cache': '缓存管理',
  'permission:view': '查看权限',
  'permission:manage': '权限管理',
  'role:view': '查看角色',
  'role:manage': '角色管理',
  'dept:view': '查看部门',
  'dept:manage': '部门管理'
}
