// 数字债权服务系统 - 统一API服务入口

// 合同管理API
export * as contractApi from './credit/contract'

// 债权管理API  
export * as creditApi from './credit/credit'

// 融资管理API
export * as financingApi from './credit/financing'

// 支付结算API
export * as paymentApi from './payment/payment'

// 区块链集成API
export * as blockchainApi from './blockchain/blockchain'

// 报表分析API
export * as reportsApi from './reports/reports'

// 安全管理API
export * as securityApi from './security/security'

// 通知服务API
export * as notificationApi from './notification/notification'

// 数字债权角色权限API
export * as digitalCreditRoleApi from './digitalCredit/role'

// RuoYi系统API (已存在的)
export * as loginApi from './login'
export * as menuApi from './menu'
export * as systemRoleApi from './system/role'
export * as systemMenuApi from './system/menu'
export * as systemUserApi from './system/user'

// API服务配置
export const API_CONFIG = {
  // 基础配置
  BASE_URL: process.env.VUE_APP_BASE_API || '/dev-api',
  TIMEOUT: 30000,
  
  // 数字债权业务API前缀
  DIGITAL_CREDIT_PREFIX: '/api',
  
  // RuoYi系统API前缀  
  RUOYI_SYSTEM_PREFIX: '/dev-api/system',
  
  // 文件上传配置
  UPLOAD_CONFIG: {
    maxSize: 10 * 1024 * 1024, // 10MB
    allowedTypes: ['.pdf', '.doc', '.docx', '.jpg', '.png', '.jpeg'],
    uploadUrl: '/api/common/upload'
  },
  
  // 导出配置
  EXPORT_CONFIG: {
    timeout: 60000, // 导出超时时间
    formats: ['pdf', 'excel', 'csv']
  }
}

// 统一错误处理配置
export const ERROR_CONFIG = {
  // 业务错误码
  BUSINESS_ERROR_CODES: {
    PERMISSION_DENIED: 'B001',
    DATA_NOT_FOUND: 'B002', 
    INVALID_OPERATION: 'B003',
    DUPLICATE_DATA: 'B004'
  },
  
  // 系统错误码
  SYSTEM_ERROR_CODES: {
    NETWORK_ERROR: 'S001',
    SERVER_ERROR: 'S002',
    TIMEOUT_ERROR: 'S003',
    AUTH_ERROR: 'S004'
  }
}

// 权限验证辅助函数
export const PermissionHelper = {
  /**
   * 检查是否有指定权限
   * @param {string} permission - 权限标识
   * @param {Array} userPermissions - 用户权限列表
   * @returns {boolean}
   */
  hasPermission(permission, userPermissions = []) {
    return userPermissions.includes(permission) || userPermissions.includes('*')
  },
  
  /**
   * 检查是否有任一权限
   * @param {Array} permissions - 权限列表
   * @param {Array} userPermissions - 用户权限列表
   * @returns {boolean}
   */
  hasAnyPermission(permissions, userPermissions = []) {
    return permissions.some(permission => this.hasPermission(permission, userPermissions))
  },
  
  /**
   * 检查是否有全部权限
   * @param {Array} permissions - 权限列表
   * @param {Array} userPermissions - 用户权限列表
   * @returns {boolean}
   */
  hasAllPermissions(permissions, userPermissions = []) {
    return permissions.every(permission => this.hasPermission(permission, userPermissions))
  }
}

// 数据范围过滤辅助函数
export const DataScopeHelper = {
  /**
   * 根据数据范围过滤查询参数
   * @param {Object} query - 原始查询参数
   * @param {string} dataScope - 数据范围
   * @param {Object} userInfo - 用户信息
   * @returns {Object} 过滤后的查询参数
   */
  filterQueryByDataScope(query, dataScope, userInfo) {
    const filteredQuery = { ...query }
    
    switch (dataScope) {
      case 'own_company':
        filteredQuery.companyId = userInfo.companyId
        break
      case 'related_contracts':
        filteredQuery.relatedToUser = userInfo.userId
        break
      case 'financing_related':
        filteredQuery.financingRelated = true
        filteredQuery.institutionId = userInfo.companyId
        break
      case 'platform_level':
        // 平台级数据不需要额外过滤
        break
      case 'all':
      default:
        // 全部数据权限不需要过滤
        break
    }
    
    return filteredQuery
  }
}

// API响应数据标准化处理
export const ResponseHelper = {
  /**
   * 标准化API响应数据
   * @param {Object} response - API响应
   * @returns {Object} 标准化后的响应
   */
  normalize(response) {
    return {
      success: response.code === 200,
      data: response.data || response.rows || null,
      message: response.msg || response.message || '',
      total: response.total || 0,
      code: response.code
    }
  },
  
  /**
   * 处理分页数据
   * @param {Object} response - API响应
   * @returns {Object} 分页数据
   */
  handlePagination(response) {
    const normalized = this.normalize(response)
    return {
      ...normalized,
      pagination: {
        current: response.pageNum || 1,
        pageSize: response.pageSize || 10,
        total: response.total || 0
      }
    }
  }
}

// 导出默认配置
export default {
  API_CONFIG,
  ERROR_CONFIG,
  PermissionHelper,
  DataScopeHelper,
  ResponseHelper
}