#!/bin/bash

# AggRoot 项目 Linux/macOS 安装脚本

set -e

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

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}    AggRoot 项目 Linux/macOS 安装脚本${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# 检查 Node.js
if ! command -v node &> /dev/null; then
    echo -e "${RED}❌ 错误: Node.js 未安装${NC}"
    echo ""
    echo "请先安装 Node.js (版本 >= 20.0.0)"
    echo "Linux: 使用 nvm 或包管理器安装"
    echo "macOS: 使用 brew install node 或从官网下载"
    echo "官网: https://nodejs.org/"
    echo ""
    exit 1
fi

NODE_VERSION=$(node -v)
echo -e "${GREEN}✅ Node.js 版本: $NODE_VERSION${NC}"
echo ""

# 检查 npm
if ! command -v npm &> /dev/null; then
    echo -e "${RED}❌ 错误: npm 未安装${NC}"
    echo ""
    exit 1
fi

echo -e "${YELLOW}📦 开始安装 AggRoot 项目...${NC}"
echo ""

# 步骤 1: 安装依赖
echo -e "${YELLOW}[1/3] 安装依赖...${NC}"
if ! npm install; then
    echo -e "${YELLOW}⚠️ 警告: 依赖安装遇到问题${NC}"
    echo "尝试使用淘宝镜像..."
    npm config set registry https://registry.npmmirror.com
    if ! npm install; then
        echo -e "${RED}❌ 错误: 依赖安装失败${NC}"
        echo "请检查网络连接或手动安装"
        exit 1
    fi
fi
echo -e "${GREEN}✅ 依赖安装完成${NC}"
echo ""

# 步骤 2: 重建原生模块
echo -e "${YELLOW}[2/3] 重建原生模块...${NC}"
echo "注意: 这可能需要几分钟时间..."
if ! npm run rebuild:linux; then
    echo -e "${YELLOW}⚠️ 警告: Linux 特定重建失败，尝试标准重建...${NC}"
    if ! npm run rebuild-native; then
        echo -e "${YELLOW}⚠️ 警告: 原生模块重建失败${NC}"
        echo "如果项目运行正常，可以忽略此警告"
        echo "如果遇到模块错误，请安装构建工具:"
        echo "  Ubuntu/Debian: sudo apt install build-essential python3 make g++"
        echo "  CentOS/RHEL: sudo yum groupinstall 'Development Tools'"
        echo "  macOS: xcode-select --install"
    else
        echo -e "${GREEN}✅ 原生模块重建完成${NC}"
    fi
else
    echo -e "${GREEN}✅ 原生模块重建完成${NC}"
fi
echo ""

# 步骤 3: 安装浏览器
echo -e "${YELLOW}[3/4] 安装 Playwright 浏览器...${NC}"
export PLAYWRIGHT_DOWNLOAD_HOST=https://npmmirror.com/mirrors/playwright
if ! npx playwright install chromium; then
    echo -e "${YELLOW}淘宝镜像失败，尝试官方源...${NC}"
    unset PLAYWRIGHT_DOWNLOAD_HOST
    if ! npx playwright install chromium; then
        echo -e "${YELLOW}⚠️ 警告: 浏览器安装失败，将在首次使用时自动下载${NC}"
    else
        echo -e "${GREEN}✅ 浏览器安装完成${NC}"
    fi
else
    echo -e "${GREEN}✅ 浏览器安装完成${NC}"
fi
echo ""

# 步骤 4: 构建项目
echo -e "${YELLOW}[4/4] 构建项目...${NC}"
if ! npm run build:dev; then
    echo -e "${RED}❌ 错误: 构建失败${NC}"
    echo "请检查 TypeScript 配置"
    exit 1
fi
echo -e "${GREEN}✅ 项目构建完成${NC}"
echo ""

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}            🎉 安装完成！${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
echo "运行以下命令启动项目："
echo ""
echo "  npm run dev    # 开发模式（热重载）"
echo "  npm start      # 生产模式"
echo ""
echo "其他有用命令："
echo "  npm test       # 运行测试"
echo "  npm run lint   # 代码检查"
echo "  npm run format # 代码格式化"
echo ""
echo "查看 README.md 获取详细文档"
echo "查看 INSTALL.md 获取安装说明"
echo ""
echo -e "${BLUE}========================================${NC}"