import request from '@/utils/request'

// 数字债权权限管理API服务

/**
 * 获取当前用户的数字债权权限信息
 * @returns {Promise} API响应
 */
export function getCurrentUserPermissions() {
  return request({
    url: '/api/digitalCredit/permissions/current',
    method: 'get'
  })
}

/**
 * 验证用户权限
 * @param {Object} data - 权限验证数据
 * @param {string} data.permission - 权限标识
 * @param {string} data.resource - 资源标识（可选）
 * @returns {Promise} API响应
 */
export function validatePermission(data) {
  return request({
    url: '/api/digitalCredit/permissions/validate',
    method: 'post',
    data: data
  })
}

/**
 * 获取用户可访问的菜单列表
 * @returns {Promise} API响应
 */
export function getUserMenus() {
  return request({
    url: '/api/digitalCredit/menus/user',
    method: 'get'
  })
}

/**
 * 获取角色权限配置
 * @param {string} roleId - 角色ID
 * @returns {Promise} API响应
 */
export function getRolePermissions(roleId) {
  return request({
    url: `/api/digitalCredit/permissions/role/${roleId}`,
    method: 'get'
  })
}

/**
 * 更新角色权限
 * @param {string} roleId - 角色ID
 * @param {Object} data - 权限数据
 * @param {Array} data.permissions - 权限列表
 * @param {string} data.dataScope - 数据范围
 * @returns {Promise} API响应
 */
export function updateRolePermissions(roleId, data) {
  return request({
    url: `/api/digitalCredit/permissions/role/${roleId}`,
    method: 'put',
    data: data
  })
}

/**
 * 获取数据范围权限配置
 * @param {string} userId - 用户ID（可选）
 * @returns {Promise} API响应
 */
export function getDataScopePermissions(userId) {
  const url = userId 
    ? `/api/digitalCredit/data-scope/${userId}`
    : '/api/digitalCredit/data-scope/current'
  
  return request({
    url: url,
    method: 'get'
  })
}

/**
 * 检查按钮权限
 * @param {Object} data - 按钮权限检查数据
 * @param {string} data.buttonCode - 按钮代码
 * @param {string} data.pageCode - 页面代码
 * @returns {Promise} API响应
 */
export function checkButtonPermission(data) {
  return request({
    url: '/api/digitalCredit/permissions/button',
    method: 'post',
    data: data
  })
}

/**
 * 获取功能模块权限列表
 * @returns {Promise} API响应
 */
export function getModulePermissions() {
  return request({
    url: '/api/digitalCredit/permissions/modules',
    method: 'get'
  })
}

/**
 * 记录权限使用日志
 * @param {Object} data - 日志数据
 * @param {string} data.permission - 使用的权限
 * @param {string} data.resource - 访问的资源
 * @param {string} data.action - 执行的操作
 * @returns {Promise} API响应
 */
export function logPermissionUsage(data) {
  return request({
    url: '/api/digitalCredit/permissions/log',
    method: 'post',
    data: data
  })
}

// 权限常量定义
export const PERMISSIONS = {
  // 合同管理权限
  CONTRACT: {
    VIEW: 'contract:view',
    CREATE: 'contract:create',
    EDIT: 'contract:edit',
    DELETE: 'contract:delete',
    CONFIRM: 'contract:confirm',
    UPLOAD: 'contract:upload',
    EXPORT: 'contract:export'
  },
  
  // 债权管理权限
  CREDIT: {
    VIEW: 'credit:view',
    GENERATE: 'credit:generate',
    TRANSFER: 'credit:transfer',
    VALIDATE: 'credit:validate',
    SIGNATURE: 'credit:signature',
    EXPORT: 'credit:export'
  },
  
  // 融资管理权限
  FINANCING: {
    VIEW: 'financing:view',
    APPLY: 'financing:apply',
    APPROVE: 'financing:approve',
    RISK_ASSESS: 'financing:risk_assess',
    DISBURSE: 'financing:disburse',
    EXPORT: 'financing:export'
  },
  
  // 支付结算权限
  PAYMENT: {
    VIEW: 'payment:view',
    PROCESS: 'payment:process',
    MANAGE: 'payment:manage',
    ESCROW_CREATE: 'payment:escrow_create',
    FUND_DISTRIBUTE: 'payment:fund_distribute',
    EXPORT: 'payment:export'
  },
  
  // 区块链集成权限
  BLOCKCHAIN: {
    VIEW: 'blockchain:view',
    MANAGE: 'blockchain:manage',
    STORE: 'blockchain:store',
    VERIFY: 'blockchain:verify',
    CONTRACT_EXECUTE: 'blockchain:contract_execute',
    CONTRACT_DEPLOY: 'blockchain:contract_deploy'
  },
  
  // 报表分析权限
  REPORTS: {
    VIEW: 'reports:view',
    GENERATE: 'reports:generate',
    EXPORT: 'reports:export',
    FINANCIAL: 'reports:financial',
    RISK_ANALYSIS: 'reports:risk_analysis',
    DASHBOARD: 'reports:dashboard'
  },
  
  // 安全管理权限
  SECURITY: {
    MANAGE: 'security:manage',
    AUDIT_LOG: 'security:audit_log',
    MFA_MANAGE: 'security:mfa_manage',
    POLICY_CONFIG: 'security:policy_config',
    USER_PERMISSION: 'security:user_permission'
  },
  
  // 通知服务权限
  NOTIFICATION: {
    VIEW: 'notification:view',
    SEND: 'notification:send',
    TEMPLATE_MANAGE: 'notification:template_manage',
    BATCH_SEND: 'notification:batch_send',
    STATS: 'notification:stats'
  }
}

// 数据范围权限常量
export const DATA_SCOPES = {
  ALL: 'all',                           // 全部数据权限
  OWN_COMPANY: 'own_company',           // 本公司数据权限
  RELATED_CONTRACTS: 'related_contracts', // 相关合同数据权限
  FINANCING_RELATED: 'financing_related', // 融资相关数据权限
  PLATFORM_LEVEL: 'platform_level'      // 平台级数据权限
}

// 角色权限映射
export const ROLE_PERMISSIONS = {
  // 安保公司权限
  SECURITY_COMPANY: [
    PERMISSIONS.CONTRACT.VIEW,
    PERMISSIONS.CONTRACT.CREATE,
    PERMISSIONS.CONTRACT.EDIT,
    PERMISSIONS.CONTRACT.UPLOAD,
    PERMISSIONS.CREDIT.VIEW,
    PERMISSIONS.CREDIT.GENERATE,
    PERMISSIONS.CREDIT.VALIDATE,
    PERMISSIONS.FINANCING.VIEW,
    PERMISSIONS.FINANCING.APPLY,
    PERMISSIONS.REPORTS.VIEW,
    PERMISSIONS.REPORTS.DASHBOARD
  ],
  
  // 甲方单位权限
  CLIENT_COMPANY: [
    PERMISSIONS.CONTRACT.VIEW,
    PERMISSIONS.CONTRACT.CONFIRM,
    PERMISSIONS.PAYMENT.VIEW,
    PERMISSIONS.PAYMENT.PROCESS,
    PERMISSIONS.REPORTS.VIEW,
    PERMISSIONS.REPORTS.DASHBOARD
  ],
  
  // 金融机构权限
  FINANCIAL_INSTITUTION: [
    PERMISSIONS.FINANCING.VIEW,
    PERMISSIONS.FINANCING.APPROVE,
    PERMISSIONS.FINANCING.RISK_ASSESS,
    PERMISSIONS.FINANCING.DISBURSE,
    PERMISSIONS.PAYMENT.VIEW,
    PERMISSIONS.PAYMENT.MANAGE,
    PERMISSIONS.PAYMENT.FUND_DISTRIBUTE,
    PERMISSIONS.REPORTS.VIEW,
    PERMISSIONS.REPORTS.FINANCIAL,
    PERMISSIONS.REPORTS.RISK_ANALYSIS,
    PERMISSIONS.REPORTS.DASHBOARD
  ],
  
  // 系统管理员权限
  ADMINISTRATOR: Object.values(PERMISSIONS).flatMap(module => Object.values(module)),
  
  // 平台运营权限
  PLATFORM_OPERATOR: [
    PERMISSIONS.BLOCKCHAIN.VIEW,
    PERMISSIONS.BLOCKCHAIN.MANAGE,
    PERMISSIONS.REPORTS.VIEW,
    PERMISSIONS.REPORTS.GENERATE,
    PERMISSIONS.REPORTS.EXPORT,
    PERMISSIONS.NOTIFICATION.VIEW,
    PERMISSIONS.NOTIFICATION.SEND,
    PERMISSIONS.NOTIFICATION.TEMPLATE_MANAGE
  ]
}