#!/bin/bash
# ============================================
# AI大模型与Agent实战入门 - 环境一键配置脚本
# 支持：Ubuntu / Debian / WSL2
# ============================================

set -e  # 遇到错误立即退出

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

echo ""
echo "╔═══════════════════════════════════════════════════════════╗"
echo "║          AI大模型与Agent实战入门 - 环境一键配置            ║"
echo "║                  支持 Ubuntu / Debian / WSL2              ║"
echo "╚═══════════════════════════════════════════════════════════╝"
echo ""

# 检测系统
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$NAME
    VER=$VERSION_ID
    print_info "检测到系统: $OS $VER"
else
    print_error "无法检测操作系统"
    exit 1
fi

echo ""
read -p "按回车键开始配置，或按Ctrl+C退出..." -n1 -s
echo ""
echo ""

# ============================================
# 步骤1: 更新系统
# ============================================
print_info " [1/7] 更新系统软件包..."
sudo apt update -y && sudo apt upgrade -y
print_success "系统更新完成"

# ============================================
# 步骤2: 安装基础工具
# ============================================
print_info " [2/7] 安装基础工具 (git, curl, wget, build-essential)..."
sudo apt install -y git curl wget vim nano build-essential

# 验证安装
git --version && print_success "Git 安装成功" || print_error "Git 安装失败"

# ============================================
# 步骤3: 安装Python 3.10+
# ============================================
print_info " [3/7] 安装 Python 3.10+..."

# 检查是否已有Python 3.10+
if command -v python3.10 &> /dev/null; then
    print_success "Python 3.10 已存在"
elif command -v python3.11 &> /dev/null; then
    print_success "Python 3.11 已存在"
else
    # 安装Python 3.10
    sudo apt install -y python3.10 python3.10-pip python3.10-venv
    print_success "Python 3.10 安装完成"
fi

# 设置Python别名
echo 'alias python=python3' >> ~/.bashrc
echo 'alias pip=pip3' >> ~/.bashrc

# 验证
python3 --version && print_success "Python 验证成功"

# ============================================
# 步骤4: 配置pip国内源
# ============================================
print_info " [4/7] 配置 pip 国内源 (清华大学)..."
mkdir -p ~/.config/pip
cat > ~/.config/pip/pip.conf << 'EOF'
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF
print_success "pip 源配置完成"

# ============================================
# 步骤5: 安装 Hermes Agent
# ============================================
print_info " [5/7] 安装 Hermes Agent..."
pip3 install hermes-agent --user
print_success "Hermes Agent 安装完成"

# 配置PATH
if [[ ":$PATH:" != *":$HOME/.local/bin:"* ]]; then
    print_info "将 $HOME/.local/bin 添加到 PATH..."
    echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
    export PATH="$HOME/.local/bin:$PATH"
fi

# 验证安装
if command -v hermes &> /dev/null; then
    hermes --version
    print_success "Hermes Agent 验证成功"
else
    print_warning "hermes命令未找到，请稍后手动执行: source ~/.bashrc"
fi

# ============================================
# 步骤6: 配置 Git
# ============================================
print_info " [6/7] 配置 Git (如果需要)..."

if ! git config --global user.name &> /dev/null; then
    echo ""
    echo "请配置Git用户信息（直接回车跳过）:"
    read -p "Git 用户名: " git_name
    if [ -n "$git_name" ]; then
        git config --global user.name "$git_name"
    fi
    
    read -p "Git 邮箱: " git_email
    if [ -n "$git_email" ]; then
        git config --global user.email "$git_email"
    fi
fi
print_success "Git 配置完成"

# ============================================
# 步骤7: 创建工作目录
# ============================================
print_info " [7/7] 创建工作目录..."
mkdir -p ~/ai-agent-workspace
print_success "工作目录已创建: ~/ai-agent-workspace"

# ============================================
# 完成
# ============================================
echo ""
echo "╔═══════════════════════════════════════════════════════════╗"
echo "║                    🎉  配置完成！                           ║"
echo "╚═══════════════════════════════════════════════════════════╝"
echo ""
echo "下一步操作:"
echo ""
echo "1. 刷新环境变量:"
echo "   source ~/.bashrc"
echo ""
echo "2. 验证 Hermes 安装:"
echo "   hermes --version"
echo ""
echo "3. 初始化配置向导:"
echo "   hermes setup"
echo ""
echo "4. 准备好你的 API Key (推荐 DeepSeek):"
echo "   访问: https://platform.deepseek.com/"
echo ""
echo "5. 进入工作目录:"
echo "   cd ~/ai-agent-workspace"
echo ""
echo "═════════════════════════════════════════════════════════════"
echo ""
echo "如果遇到问题，请参考学员手册中的常见问题FAQ"
echo ""
