#!/bin/bash

# AI Agent Collaboration Framework - 安装脚本
# 一键安装，快速启动多Agent协作

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"
}

# 检查系统要求
check_requirements() {
    print_info "检查系统要求..."
    
    # 检查Python
    if ! command -v python3 &> /dev/null; then
        print_error "Python3 未安装，请先安装Python3"
        exit 1
    fi
    
    # 检查Git
    if ! command -v git &> /dev/null; then
        print_error "Git 未安装，请先安装Git"
        exit 1
    fi
    
    # 检查SSH
    if ! command -v ssh &> /dev/null; then
        print_error "SSH 未安装，请先安装SSH"
        exit 1
    fi
    
    print_success "系统要求检查通过"
}

# 创建目录结构
create_directories() {
    print_info "创建项目目录结构..."
    
    mkdir -p agents/{keys,configs,templates}
    mkdir -p src/{identity,cli,web}
    mkdir -p tests/{unit,integration}
    mkdir -p examples/{basic,advanced}
    mkdir -p docs/{api,guides}
    mkdir -p scripts
    
    print_success "目录结构创建完成"
}

# 安装Python依赖
install_python_deps() {
    print_info "安装Python依赖..."
    
    python3 -m pip install --upgrade pip
    pip install -r requirements.txt
    
    print_success "Python依赖安装完成"
}

# 初始化Agent身份
initialize_agents() {
    print_info "初始化Agent身份..."
    
    # 复制身份管理器
    cp agent_identity_manager.py src/identity/
    cp demo_collaboration.py examples/basic/
    
    # 运行Agent设置
    if [ -f "setup_agents.sh" ]; then
        chmod +x setup_agents.sh
        ./setup_agents.sh
    else
        print_warning "setup_agents.sh 未找到，跳过Agent初始化"
    fi
    
    print_success "Agent身份初始化完成"
}

# 设置权限
set_permissions() {
    print_info "设置文件权限..."
    
    chmod +x scripts/*.sh 2>/dev/null || true
    chmod +x agents/*.sh 2>/dev/null || true
    
    print_success "权限设置完成"
}

# 创建快捷方式
create_symlinks() {
    print_info "创建快捷方式..."
    
    # 创建全局命令（可选）
    if [ "$1" = "--global" ]; then
        sudo ln -sf "$(pwd)/agents/switch_agent.sh" /usr/local/bin/agent-switch
        sudo ln -sf "$(pwd)/agents/stats.sh" /usr/local/bin/agent-stats
        print_success "全局命令已创建"
    fi
}

# 验证安装
verify_installation() {
    print_info "验证安装..."
    
    # 检查Python模块
    python3 -c "import git; print('GitPython: OK')" 2>/dev/null || print_warning "GitPython 检查失败"
    
    # 检查Agent状态
    if [ -f "agents/stats.sh" ]; then
        ./agents/stats.sh
    fi
    
    print_success "安装验证完成"
}

# 显示使用说明
show_usage() {
    print_success "🎉 AI Agent Collaboration Framework 安装完成！"
    echo
    echo "使用方法:"
    echo "  1. 运行演示: python3 examples/basic/demo_collaboration.py"
    echo "  2. 查看Agent: ./agents/stats.sh"
    echo "  3. 切换Agent: ./agents/switch_agent.sh claude-ai"
    echo "  4. 快速开始: cat QUICK_START.md"
    echo
    echo "文档:"
    echo "  - 快速开始: ./docs/quick_start.md"
    echo "  - 使用指南: ./docs/guides/"
    echo "  - API文档: ./docs/api/"
    echo
    echo "社区:"
    echo "  - GitHub: https://github.com/your-org/agent-collaboration-framework"
    echo "  - 讨论区: https://github.com/your-org/agent-collaboration-framework/discussions"
}

# 主安装流程
main() {
    echo "========================================"
    echo "  AI Agent Collaboration Framework"
    echo "========================================"
    echo
    
    # 检查参数
    local global_install=false
    while [[ $# -gt 0 ]]; do
        case $1 in
            --global)
                global_install=true
                shift
                ;;
            --help|-h)
                echo "使用方法: $0 [--global] [--help]"
                echo "  --global: 创建全局命令"
                echo "  --help:   显示帮助"
                exit 0
                ;;
            *)
                print_error "未知参数: $1"
                exit 1
                ;;
        esac
    done
    
    # 执行安装流程
    check_requirements
    create_directories
    install_python_deps
    initialize_agents
    set_permissions
    create_symlinks $global_install
    verify_installation
    
    show_usage
}

# 运行主程序
main "$@"