const getApiBaseUrl = (): string => {
  if (import.meta.env.VITE_API_BASE_URL) {
    return import.meta.env.VITE_API_BASE_URL
  }
  return 'http://localhost:3006/api'
}

export const APP_NAME = import.meta.env.VITE_APP_NAME || 'AI磁场'

export const APP_CONFIG = {
  APP_NAME,
  API_BASE_URL: getApiBaseUrl(),
  PAGES: {
    INDEX: '/pages/index/index',
    INTERVIEW: '/pages/interview/interview',
    REPORT: '/pages/report/report',
    RESUME: '/pages/resume/resume',
    HISTORY: '/pages/history/history',
    MEMBER: '/pages/member/member',
    PROGRESS: '/pages/progress/progress',
    CONTRIBUTE: '/pages/contribute/contribute',
    INTERNSHIP: '/pages/internship/internship',
    USER: '/pages/user/user',
    LOGIN: '/pages/login/login',
    ABOUT: '/pages/about/about',
    CAREER: '/pages/career/career',
  },
  STORAGE_KEYS: {
    TOKEN: 'token',
    USER_ID: 'userId',
    RESUME: 'resume',
  },
} as const

export const API_ENDPOINTS = {
  USER: {
    SEND_CODE: '/user/send-code',
    LOGIN: '/user/login',
    WX_LOGIN: '/user/wx-login',
    INFO: '/user/info',
    UPDATE: '/user/update',
    USAGE: '/user/usage',
  },
  INTERVIEW: {
    CREATE: '/interview/create',
    ANSWER: (id: string) => `/interview/${id}/answer`,
    COMPLETE: (id: string) => `/interview/${id}/complete`,
    GET: (id: string) => `/interview/${id}`,
    LIST: '/interview/list/all',
    STATS: '/interview/stats/mine',
  },
  ANALYZE: {
    DIAGNOSIS: '/analyze/diagnosis',
    OPTIMIZE: '/analyze/optimize',
    SKILLS_GAP: '/analyze/skills-gap',
  },
  RESUME: {
    CREATE: '/resume/create',
    LIST: '/resume/list',
    GET: (id: string) => `/resume/${id}`,
    DELETE: (id: string) => `/resume/${id}`,
  },
  PROGRESS: {
    GET: '/progress',
    STATS: '/progress/stats',
  },
  CONTRIBUTION: {
    CREATE: '/contribution',
    MY: '/contribution/my',
    BANK: (company: string, position: string) => `/contribution/company/${company}/position/${position}`,
    COMPANY: (company: string) => `/contribution/company/${company}`,
  },
  MEMBER: {
    PLANS: '/member/plans',
    STATUS: '/member/status',
    PAY: '/member/pay',
    SPRINT_DEDUCT: '/member/sprint/deduct',
  },
  DAILY_QUESTION: {
    TODAY: '/daily-question',
    BY_POSITION: (position: string) => `/daily-question/position/${position}`,
  },
  ADMIN: {
    CHECK: '/admin/check',
    OVERVIEW: '/admin/overview',
    USERS: '/admin/users',
    SET_VIP: '/admin/set-vip',
    CONFIG: '/admin/config',
    CONFIG_SAVE: '/admin/config/save',
    PRICING: '/admin/pricing',
    PRICING_SAVE: '/admin/pricing/save',
    ORDERS: '/admin/orders',
    ORDER_SYNC: '/admin/order/sync',
  },
  PAYMENT: {
    CREATE: '/payment/create',
    JSAPI: '/payment/jsapi',
    QUERY: '/payment/query',
    CHECK: (outTradeNo: string) => `/payment/check/${outTradeNo}`,
    ACTIVATE: '/payment/activate',
  },
  TTS: {
    SYNTHESIZE: '/tts/synthesize',
    AUDIO: (hash: string) => `/tts/audio/${hash}`,
    ASR: '/tts/asr',
  },
  SHARE: {
    CREATE: '/share/create',
    STATS: '/share/stats',
    RECORDS: '/share/records',
    VISITORS: '/share/visitors',
  },
  REVIEW: {
    UPLOAD: '/interview-review',
    TEXT: '/interview-review/text',
    LIST: '/interview-review/list',
    DETAIL: (id: string) => `/interview-review/${id}`,
    DELETE: (id: string) => `/interview-review/${id}`,
  },
  CAREER: {
    ANALYZE: '/career-advice/analyze',
    CHAT: '/career-advice/chat',
    POSITIONS: '/career-advice/positions',
  },
  VIRTUAL_PAYMENT: {
    CREATE: '/virtual-payment/create',
    CHECK: (outTradeNo: string) => `/virtual-payment/check/${outTradeNo}`,
  },
} as const

const PROD_API_HOST = import.meta.env.VITE_PROD_API_HOST || 'https://zhiyinwx.yzrcloud.cn'
const DEV_API_HOST = 'http://localhost:3006'

let API_HOST: string
// #ifdef H5
API_HOST = window.location.hostname !== 'localhost' && window.location.hostname !== '127.0.0.1'
  ? window.location.origin
  : DEV_API_HOST
// #endif
// #ifdef MP-WEIXIN
API_HOST = PROD_API_HOST
// #endif
// #ifndef H5 && !MP-WEIXIN
API_HOST = PROD_API_HOST
// #endif

export function api(path: string): string {
  return `${API_HOST}/api${path}`
}

export default APP_CONFIG
