/**
 * API配置
 */

// 判断是否为开发环境
// @ts-ignore
const isDevelopment = import.meta.env.DEV || false;

/** 客服电话 */
export const CUSTOMER_SERVICE_PHONE = '400-0755-315';

/** 获取小程序真实版本号 */
export const getAppVersion = (): string => {
  try {
    // #ifdef MP-WEIXIN
    const accountInfo = wx.getAccountInfoSync();
    const { version, envVersion } = accountInfo.miniProgram;
    if (envVersion === 'release') {
      return `v${version}`;
    }
    if (envVersion === 'trial') {
      return '体验版';
    }
    return '开发版';
    // #endif
  } catch {
    // 非微信小程序环境或调用失败时回退
    return 'v1.0.0';
  }
  // 非微信小程序编译目标时回退
  // #ifndef MP-WEIXIN
  return 'v1.0.0';
  // #endif
};

export const API_CONFIG = {
  // 后端API基础URL
  // 注意：上线生产环境时需替换为正式域名
  baseUrl: 'https://dev-haha.kuaiyuman.cn/api',
  // baseUrl: 'http://192.168.10.100:7077/api',  // 本地开发
  // baseUrl: 'http://localhost:7077/api',  // 本地开发

  // 请求超时时间（毫秒）
  timeout: 10000,

  // 是否显示请求日志
  enableLog: isDevelopment
};

/**
 * API接口路径
 * 各 API 文件从此处引用路径常量，避免硬编码
 */
export const API_PATHS = {
  // 用户相关
  loginMiniappPhone: '/login/miniapp-phone',
  getUserInfo: '/login/user-info',
  logout: '/login/logout',
  
  // 设备相关
  scanDoor: '/device/scan-open',
  checkDeviceStatus: '/device/status',
  getDeviceProducts: (deviceId: string) => `/device/products/${deviceId}`,
  
  // 订单相关
  getOrders: '/order/list',
  getOrderDetail: '/order/detail',
  cancelOrder: '/order/cancel',
  
  // 支付分相关
  preCreatePayscoreOrder: '/payscore/pre-create',
  createPayscoreOrder: '/payscore/create',
  modifyPayscoreOrder: '/payscore/modify',
  syncPayscoreOrderInfo: (orderId: number) => `/payscore/sync-info/${orderId}`,
  refundPayscoreOrder: (orderId: number) => `/payscore/refund/${orderId}`,
  queryPayscoreRefund: (orderId: number, outRefundNo: string) => `/payscore/refund/${orderId}/${outRefundNo}`,
  queryPayscoreByOutNo: (outOrderNo: string) => `/payscore/query-out-no/${outOrderNo}`,
  getPayscoreDetailSign: '/payscore/detail-sign',
  
  // 优惠券相关
  getMyCoupons: '/coupon/my',
  getCouponDetail: (id: string) => `/coupon/${id}`,
  receiveCoupon: (templateId: string) => `/coupon/receive/${templateId}`,
  getCouponCount: '/coupon/count',
  getAvailableCoupons: '/coupon/available',
  getUsableCoupons: '/coupon/usable',
  
  // 状态轮询相关
  pollDeviceStatus: '/status/device',
  pollRecognizeResult: '/status/recognize',
  pollOrderInfo: '/status/order',
  pollAllStatus: '/status/all',
  
  // 公告相关
  getAnnouncementList: '/announcement/list',
  getAnnouncementDetail: '/announcement/detail',
};
