// API接口配置
const API_CONFIG = {
  // 基础配置
  BASE_URL: {
    development: 'https://dev-api.xlxumu.com',
    testing: 'https://test-api.xlxumu.com',
    production: 'https://api.xlxumu.com'
  },
  
  // 超时时间
  TIMEOUT: 10000,
  
  // 请求头
  HEADERS: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  }
}

// API接口地址
const API_ENDPOINTS = {
  // 认证相关
  AUTH: {
    LOGIN: '/auth/login',
    LOGOUT: '/auth/logout',
    REGISTER: '/auth/register',
    REFRESH_TOKEN: '/auth/refresh',
    VALIDATE_TOKEN: '/auth/validate',
    RESET_PASSWORD: '/auth/reset-password',
    CHANGE_PASSWORD: '/auth/change-password'
  },
  
  // 用户相关
  USER: {
    PROFILE: '/user/profile',
    UPDATE_PROFILE: '/user/profile',
    AVATAR_UPLOAD: '/user/avatar',
    SETTINGS: '/user/settings'
  },
  
  // 养殖管理
  FARMING: {
    STATS: '/farming/stats',
    ACTIVITIES: '/farming/activities',
    FARMS: '/farming/farms',
    FARM_DETAIL: '/farming/farms/:id',
    ANIMALS: '/farming/animals',
    ANIMAL_DETAIL: '/farming/animals/:id',
    HEALTH_RECORDS: '/farming/health-records',
    FEED_RECORDS: '/farming/feed-records',
    PRODUCTION_RECORDS: '/farming/production-records'
  },
  
  // 牛只交易
  TRADING: {
    PRODUCTS: '/trading/products',
    PRODUCT_DETAIL: '/trading/products/:id',
    HOT_PRODUCTS: '/trading/products/hot',
    LATEST_PRODUCTS: '/trading/products/latest',
    CATEGORIES: '/trading/categories',
    SEARCH: '/trading/search',
    PUBLISH: '/trading/publish',
    ORDERS: '/trading/orders',
    ORDER_DETAIL: '/trading/orders/:id'
  },
  
  // 牛肉商城
  MALL: {
    PRODUCTS: '/mall/products',
    PRODUCT_DETAIL: '/mall/products/:id',
    CATEGORIES: '/mall/categories',
    CART: '/mall/cart',
    ORDERS: '/mall/orders',
    ORDER_DETAIL: '/mall/orders/:id',
    ADDRESSES: '/mall/addresses',
    PAYMENT: '/mall/payment'
  },
  
  // 银行监管
  BANK: {
    DASHBOARD: '/bank/dashboard',
    LOANS: '/bank/loans',
    LOAN_DETAIL: '/bank/loans/:id',
    LOAN_APPROVE: '/bank/loans/:id/approve',
    RISK_ASSESSMENT: '/bank/risk/assessment',
    RISK_MONITOR: '/bank/risk/monitor',
    COMPLIANCE_CHECK: '/bank/compliance/check',
    AUDIT_REPORTS: '/bank/audit/reports'
  },
  
  // 保险监管
  INSURANCE: {
    DASHBOARD: '/insurance/dashboard',
    POLICIES: '/insurance/policies',
    POLICY_DETAIL: '/insurance/policies/:id',
    CLAIMS: '/insurance/claims',
    CLAIM_DETAIL: '/insurance/claims/:id',
    CLAIM_PROCESS: '/insurance/claims/:id/process',
    RISK_ASSESSMENT: '/insurance/risk/assessment',
    COMPLIANCE_CHECK: '/insurance/compliance/check',
    CLAIM_ALERTS: '/insurance/claim-alerts'
  },
  
  // 通用接口
  COMMON: {
    UPLOAD: '/common/upload',
    WEATHER: '/common/weather',
    REGIONS: '/common/regions',
    DICTIONARIES: '/common/dictionaries'
  }
}

// 获取当前环境
const getCurrentEnv = () => {
  // #ifdef MP-WEIXIN
  return 'production'
  // #endif
  
  // #ifdef H5
  const hostname = window.location.hostname
  if (hostname.includes('localhost') || hostname.includes('127.0.0.1')) {
    return 'development'
  } else if (hostname.includes('test')) {
    return 'testing'
  } else {
    return 'production'
  }
  // #endif
  
  return 'production'
}

// 获取基础URL
const getBaseURL = () => {
  const env = getCurrentEnv()
  return API_CONFIG.BASE_URL[env]
}

// 构建完整URL
const buildURL = (endpoint, params = {}) => {
  let url = endpoint
  
  // 替换路径参数
  Object.keys(params).forEach(key => {
    url = url.replace(`:${key}`, params[key])
  })
  
  return url
}

export {
  API_CONFIG,
  API_ENDPOINTS,
  getCurrentEnv,
  getBaseURL,
  buildURL
}