#!/usr/bin/env bash
# =============================================================
# Claude Code + DeepSeek 一键配置脚本 (macOS)
# 用法：在终端中执行此脚本 (bash setup-claude-deepseek.sh)
# =============================================================

# 颜色定义
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'

DEEPSEEK_API_KEY="sk-a7e8782b1d5547a692ef2efec55b44f2"
DEEPSEEK_BASE_URL="https://api.deepseek.com/anthropic"
DEEPSEEK_MODEL="deepseek-v4-pro"

echo -e "${CYAN}=== Claude Code + DeepSeek 配置开始 (macOS) ===${NC}"

# ── 1. 检查 Node.js ──────────────────────────────────────────
echo -e "\n${YELLOW}[1/4] 检查 Node.js...${NC}"
if ! command -v node &> /dev/null; then
    echo -e "${YELLOW}未找到 Node.js，准备自动安装...${NC}"
    if ! command -v brew &> /dev/null; then
        echo -e "${YELLOW}未找到 Homebrew，正在为您安装 Homebrew...${NC}"
        /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
        # 初始化 brew 环境变量以防当前终端 session 暂未更新
        if [ -x "/opt/homebrew/bin/brew" ]; then
            eval "$(/opt/homebrew/bin/brew shellenv)"
        elif [ -x "/usr/local/bin/brew" ]; then
            eval "$(/usr/local/bin/brew shellenv)"
        fi
    fi
    echo -e "${YELLOW}正在通过 Homebrew 安装 Node.js...${NC}"
    export HOMEBREW_NO_AUTO_UPDATE=1
    brew install node
    if [ $? -ne 0 ]; then
        echo -e "${RED}Node.js 自动安装失败，请手动安装后重试。${NC}"
        exit 1
    fi
fi
echo -e "${GREEN}Node.js $(node --version) 已安装${NC}"

# ── 2. 安装 Claude Code ───────────────────────────────────────
echo -e "\n${YELLOW}[2/4] 安装 Claude Code...${NC}"
if command -v claude &> /dev/null; then
    echo -e "${GREEN}Claude Code 已安装：$(claude --version 2>&1)${NC}"
else
    npm install -g @anthropic-ai/claude-code
    if [ $? -ne 0 ]; then
        echo -e "${RED}安装失败，请检查 npm 网络和权限${NC}"
        exit 1
    fi
    echo -e "${GREEN}Claude Code 安装成功${NC}"
fi

# ── 3. 写入 ~/.claude/settings.json（仅保留 model） ──────────
echo -e "\n${YELLOW}[3/4] 配置 ~/.claude/settings.json...${NC}"
CLAUDE_DIR="$HOME/.claude"
mkdir -p "$CLAUDE_DIR"
cat <<EOF > "$CLAUDE_DIR/settings.json"
{
  "model": "$DEEPSEEK_MODEL"
}
EOF
echo -e "${GREEN}已写入 $CLAUDE_DIR/settings.json${NC}"

# ── 4. 设置环境变量 profile (支持 bash 和 zsh) ────────────────
echo -e "\n${YELLOW}[4/4] 设置环境变量...${NC}"

PROFILE_CONTENT=$(cat <<EOF
# Claude Code + DeepSeek Config
export ANTHROPIC_BASE_URL="$DEEPSEEK_BASE_URL"
export ANTHROPIC_AUTH_TOKEN="$DEEPSEEK_API_KEY"
export ANTHROPIC_MODEL="$DEEPSEEK_MODEL"
EOF
)

# 确定要写入的 profile 文件
if [ -n "$ZSH_VERSION" ] || [[ "$SHELL" == *"zsh"* ]]; then
    PROFILE_FILE="$HOME/.zshrc"
else
    PROFILE_FILE="$HOME/.bash_profile"
    if [ ! -f "$PROFILE_FILE" ] && [ -f "$HOME/.bashrc" ]; then
        PROFILE_FILE="$HOME/.bashrc"
    fi
fi

if grep -q "ANTHROPIC_BASE_URL" "$PROFILE_FILE" 2>/dev/null; then
    echo -e "${GREEN}环境变量已存在于 $PROFILE_FILE，建议手动检查以防重复${NC}"
else
    echo -e "\n$PROFILE_CONTENT" >> "$PROFILE_FILE"
    echo -e "${GREEN}已追加环境变量到：$PROFILE_FILE${NC}"
fi

# ── 验证连接 ──────────────────────────────────────────────────
echo -e "\n${CYAN}=== 验证连接 ===${NC}"
export ANTHROPIC_BASE_URL="$DEEPSEEK_BASE_URL"
export ANTHROPIC_AUTH_TOKEN="$DEEPSEEK_API_KEY"
export ANTHROPIC_MODEL="$DEEPSEEK_MODEL"

# 运行 claude 进行简短测试
echo -e "${YELLOW}正在尝试连接 api...${NC}"
result=$(claude -p "say hi" 2>&1)
if [ $? -eq 0 ]; then
    echo -e "${GREEN}连接成功：$result${NC}"
else
    echo -e "${RED}连接失败：$result${NC}"
fi

echo -e "\n${CYAN}=== 配置完成！请执行 'source $PROFILE_FILE' 或新开终端后直接运行 claude ===${NC}"