/**
 * 全局配置文件
 * 统一管理应用配置信息
 */

// 环境配置
const ENV_CONFIG = {
  development: {
    baseURL: 'https://dev-api.xlxumu.com',
    wsURL: 'wss://dev-ws.xlxumu.com',
    debug: true,
    logLevel: 'debug'
  },
  testing: {
    baseURL: 'https://test-api.xlxumu.com',
    wsURL: 'wss://test-ws.xlxumu.com',
    debug: true,
    logLevel: 'info'
  },
  production: {
    baseURL: 'https://api.xlxumu.com',
    wsURL: 'wss://ws.xlxumu.com',
    debug: false,
    logLevel: 'error'
  }
}

// 获取当前环境
const getCurrentEnv = () => {
  // #ifdef MP-WEIXIN
  return process.env.NODE_ENV || 'production'
  // #endif
  
  // #ifdef H5
  return process.env.NODE_ENV || 'production'
  // #endif
  
  // #ifdef APP-PLUS
  return process.env.NODE_ENV || 'production'
  // #endif
  
  return 'production'
}

// 当前环境配置
const currentEnv = getCurrentEnv()
const config = ENV_CONFIG[currentEnv] || ENV_CONFIG.production

// 应用配置
export const APP_CONFIG = {
  // 应用信息
  name: '智慧畜牧业小程序矩阵',
  version: '1.0.0',
  description: '基于uni-app开发的智慧畜牧业管理系统',
  
  // 环境配置
  env: currentEnv,
  debug: config.debug,
  logLevel: config.logLevel,
  
  // API配置
  api: {
    baseURL: config.baseURL,
    timeout: 10000,
    retryCount: 3,
    retryDelay: 1000
  },
  
  // WebSocket配置
  websocket: {
    url: config.wsURL,
    heartbeatInterval: 30000,
    reconnectInterval: 5000,
    maxReconnectAttempts: 5
  },
  
  // 存储配置
  storage: {
    prefix: 'xlxumu_',
    tokenKey: 'access_token',
    userKey: 'user_info',
    settingsKey: 'app_settings'
  },
  
  // 分页配置
  pagination: {
    defaultPageSize: 20,
    maxPageSize: 100
  },
  
  // 上传配置
  upload: {
    maxSize: 10 * 1024 * 1024, // 10MB
    allowedTypes: ['image/jpeg', 'image/png', 'image/gif'],
    quality: 0.8
  },
  
  // 地图配置
  map: {
    key: 'your-map-key',
    defaultLocation: {
      latitude: 39.908823,
      longitude: 116.397470
    }
  },
  
  // 主题配置
  theme: {
    primaryColor: '#2E8B57',
    secondaryColor: '#32CD32',
    successColor: '#67C23A',
    warningColor: '#E6A23C',
    errorColor: '#F56C6C',
    infoColor: '#909399'
  },
  
  // 缓存配置
  cache: {
    defaultExpire: 30 * 60 * 1000, // 30分钟
    maxSize: 50 * 1024 * 1024 // 50MB
  }
}

// 小程序特定配置
export const MINIPROGRAM_CONFIG = {
  // 养殖管理小程序
  'farming-manager': {
    appId: 'wx1234567890abcdef',
    name: '养殖管理',
    description: '专业的畜牧养殖管理工具',
    features: ['养殖记录', '健康监测', '饲料管理', '疫苗管理']
  },
  
  // 牛只交易小程序
  'cattle-trading': {
    appId: 'wx2345678901bcdefg',
    name: '牛只交易',
    description: '安全可靠的牛只交易平台',
    features: ['交易发布', '价格查询', '交易撮合', '支付结算']
  },
  
  // 牛肉商城小程序
  'beef-mall': {
    appId: 'wx3456789012cdefgh',
    name: '牛肉商城',
    description: '优质牛肉在线购买平台',
    features: ['商品展示', '在线下单', '配送跟踪', '售后服务']
  },
  
  // 银行监管小程序
  'bank-supervision': {
    appId: 'wx4567890123defghi',
    name: '银行监管',
    description: '畜牧业金融监管服务平台',
    features: ['贷款申请', '风险评估', '资金监管', '数据报告']
  },
  
  // 保险监管小程序
  'insurance-supervision': {
    appId: 'wx5678901234efghij',
    name: '保险监管',
    description: '畜牧业保险监管服务平台',
    features: ['保险申请', '理赔处理', '风险评估', '监管报告']
  }
}

// API接口配置
export const API_CONFIG = {
  // 用户相关
  user: {
    login: '/auth/login',
    logout: '/auth/logout',
    register: '/auth/register',
    profile: '/user/profile',
    updateProfile: '/user/profile'
  },
  
  // 养殖管理
  farming: {
    list: '/farming/list',
    detail: '/farming/detail',
    create: '/farming/create',
    update: '/farming/update',
    delete: '/farming/delete',
    records: '/farming/records',
    health: '/farming/health',
    feed: '/farming/feed',
    vaccine: '/farming/vaccine'
  },
  
  // 交易相关
  trading: {
    list: '/trading/list',
    detail: '/trading/detail',
    publish: '/trading/publish',
    bid: '/trading/bid',
    orders: '/trading/orders',
    payment: '/trading/payment'
  },
  
  // 商城相关
  mall: {
    products: '/mall/products',
    categories: '/mall/categories',
    cart: '/mall/cart',
    orders: '/mall/orders',
    payment: '/mall/payment'
  },
  
  // 金融相关
  finance: {
    loans: '/finance/loans',
    apply: '/finance/apply',
    repayment: '/finance/repayment',
    records: '/finance/records'
  },
  
  // 保险相关
  insurance: {
    policies: '/insurance/policies',
    apply: '/insurance/apply',
    claims: '/insurance/claims',
    records: '/insurance/records'
  },
  
  // 文件上传
  upload: {
    image: '/upload/image',
    file: '/upload/file',
    video: '/upload/video'
  },
  
  // 系统相关
  system: {
    config: '/system/config',
    version: '/system/version',
    feedback: '/system/feedback'
  }
}

// 错误码配置
export const ERROR_CODES = {
  // 通用错误
  SUCCESS: 0,
  UNKNOWN_ERROR: -1,
  NETWORK_ERROR: -2,
  TIMEOUT_ERROR: -3,
  
  // 认证错误
  AUTH_FAILED: 1001,
  TOKEN_EXPIRED: 1002,
  PERMISSION_DENIED: 1003,
  
  // 参数错误
  INVALID_PARAMS: 2001,
  MISSING_PARAMS: 2002,
  
  // 业务错误
  USER_NOT_FOUND: 3001,
  USER_ALREADY_EXISTS: 3002,
  INSUFFICIENT_BALANCE: 3003,
  ORDER_NOT_FOUND: 3004,
  PRODUCT_OUT_OF_STOCK: 3005
}

// 错误消息配置
export const ERROR_MESSAGES = {
  [ERROR_CODES.SUCCESS]: '操作成功',
  [ERROR_CODES.UNKNOWN_ERROR]: '未知错误',
  [ERROR_CODES.NETWORK_ERROR]: '网络连接失败',
  [ERROR_CODES.TIMEOUT_ERROR]: '请求超时',
  [ERROR_CODES.AUTH_FAILED]: '认证失败',
  [ERROR_CODES.TOKEN_EXPIRED]: '登录已过期，请重新登录',
  [ERROR_CODES.PERMISSION_DENIED]: '权限不足',
  [ERROR_CODES.INVALID_PARAMS]: '参数错误',
  [ERROR_CODES.MISSING_PARAMS]: '缺少必要参数',
  [ERROR_CODES.USER_NOT_FOUND]: '用户不存在',
  [ERROR_CODES.USER_ALREADY_EXISTS]: '用户已存在',
  [ERROR_CODES.INSUFFICIENT_BALANCE]: '余额不足',
  [ERROR_CODES.ORDER_NOT_FOUND]: '订单不存在',
  [ERROR_CODES.PRODUCT_OUT_OF_STOCK]: '商品库存不足'
}

// 常量配置
export const CONSTANTS = {
  // 性别
  GENDER: {
    MALE: 1,
    FEMALE: 2,
    UNKNOWN: 0
  },
  
  // 状态
  STATUS: {
    ACTIVE: 1,
    INACTIVE: 0,
    PENDING: 2,
    DELETED: -1
  },
  
  // 订单状态
  ORDER_STATUS: {
    PENDING: 1,
    PAID: 2,
    SHIPPED: 3,
    DELIVERED: 4,
    CANCELLED: -1,
    REFUNDED: -2
  },
  
  // 支付方式
  PAYMENT_METHOD: {
    WECHAT: 1,
    ALIPAY: 2,
    BANK_CARD: 3,
    CASH: 4
  },
  
  // 文件类型
  FILE_TYPE: {
    IMAGE: 1,
    VIDEO: 2,
    DOCUMENT: 3,
    AUDIO: 4
  }
}

// 导出默认配置
export default {
  APP_CONFIG,
  MINIPROGRAM_CONFIG,
  API_CONFIG,
  ERROR_CODES,
  ERROR_MESSAGES,
  CONSTANTS
}