import { existsSync, readFileSync } from 'node:fs'
import { homedir } from 'node:os'
import { join } from 'node:path'

function getGlobalConfigPath() {
  const configHome = process.env.CLAVUE_CONFIG_DIR || join(homedir(), '.clavue')
  return join(configHome.normalize('NFC'), '.clavue.json')
}

function getLanguage() {
  try {
    const path = getGlobalConfigPath()
    if (!existsSync(path)) return 'en'
    const parsed = JSON.parse(readFileSync(path, 'utf8'))
    return parsed?.myclaudeLanguage === 'zh' ? 'zh' : 'en'
  } catch {
    return 'en'
  }
}

export async function call() {
  const language = getLanguage()

  if (language === 'zh') {
    return {
      type: 'text',
      value:
        '当前版本的 /login 已改为启动前配置流。\n请先退出当前会话，然后重新运行 clavue；启动时会先让你选择“直接使用当前配置”或“重新配置模型与 API”。\n如果你想立刻进入配置，也可以直接在终端执行：clavue setup-token\n如果你只是想检查当前供应商与模型是否可用，可以执行：/provider validate\n如果你想自动修复无效的模型路由，可以执行：/provider repair',
    }
  }

  return {
    type: 'text',
      value:
      'The /login flow has been moved to the pre-launch configuration path.\nExit the current session and start clavue again; before chat starts you will be able to choose between using the current configuration or reconfiguring model and API settings.\nIf you want to open setup immediately from the shell, run: clavue setup-token\nIf you only want to test the current provider and model route, run: /provider validate\nIf you want Clavue to auto-repair an invalid model route, run: /provider repair',
  }
}
