/**
 * 环境配置 — 按小程序运行版本自动切换 API 地址
 *
 * develop  → 本机开发服务器（需在开发者工具中关闭域名校验）
 * trial    → 测试环境（体验版）
 * release  → 正式环境
 */

type EnvVersion = "develop" | "trial" | "release"

interface EnvConfig {
  name: string
  baseURL: string
}

function getEnvVersion(): EnvVersion {
  try {
    const info = wx.getAccountInfoSync()
    return (info?.miniProgram?.envVersion as EnvVersion) || "develop"
  } catch {
    return "develop"
  }
}

const ENV_MAP: Record<EnvVersion, EnvConfig> = {
  develop: {
    name: "开发环境",
    baseURL: "http://127.0.0.1:8000",
  },
  trial: {
    name: "测试环境",
    baseURL: "https://test-api.langlangzhuoqiu.cn",
  },
  release: {
    name: "正式环境",
    baseURL: "https://api.langlangzhuoqiu.cn",
  },
}

export const ENV_VERSION = getEnvVersion()
export const ENV_CONFIG = ENV_MAP[ENV_VERSION]
export const API_BASE = ENV_CONFIG.baseURL
