#!/bin/bash

#############################################
# AI 夜間開發系統 - 測試腳本
#############################################

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

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

echo -e "${BLUE}=========================================${NC}"
echo -e "${BLUE}   AI Dev System - 系統測試${NC}"
echo -e "${BLUE}=========================================${NC}"
echo ""

total_tests=0
passed_tests=0

test_passed() {
    echo -e "${GREEN}✓ PASSED${NC}"
    passed_tests=$((passed_tests + 1))
}

test_failed() {
    echo -e "${RED}✗ FAILED: $1${NC}"
}

run_test() {
    total_tests=$((total_tests + 1))
    echo -n "  [$total_tests] $1 ... "
}

# ============================================
# 測試 1: 腳本檔案存在
# ============================================

echo -e "${YELLOW}[測試類別] 檔案檢查${NC}"

run_test "ai-dev-loop.sh 存在"
if [ -f "$SCRIPT_DIR/ai-dev-loop.sh" ]; then
    test_passed
else
    test_failed "檔案不存在"
fi

run_test "ai-dev-control.sh 存在"
if [ -f "$SCRIPT_DIR/ai-dev-control.sh" ]; then
    test_passed
else
    test_failed "檔案不存在"
fi

run_test "project-configs.sh 存在"
if [ -f "$SCRIPT_DIR/project-configs.sh" ]; then
    test_passed
else
    test_failed "檔案不存在"
fi

run_test "install-cron.sh 存在"
if [ -f "$SCRIPT_DIR/install-cron.sh" ]; then
    test_passed
else
    test_failed "檔案不存在"
fi

# ============================================
# 測試 2: 檔案權限
# ============================================

echo ""
echo -e "${YELLOW}[測試類別] 檔案權限${NC}"

run_test "ai-dev-loop.sh 可執行"
if [ -x "$SCRIPT_DIR/ai-dev-loop.sh" ]; then
    test_passed
else
    test_failed "無執行權限"
fi

run_test "ai-dev-control.sh 可執行"
if [ -x "$SCRIPT_DIR/ai-dev-control.sh" ]; then
    test_passed
else
    test_failed "無執行權限"
fi

# ============================================
# 測試 3: 系統需求
# ============================================

echo ""
echo -e "${YELLOW}[測試類別] 系統需求${NC}"

run_test "Git 已安裝"
if command -v git &> /dev/null; then
    test_passed
else
    test_failed "未安裝 Git"
fi

run_test "Crontab 可用"
if command -v crontab &> /dev/null; then
    test_passed
else
    test_failed "Crontab 不可用"
fi

run_test "至少一個 AI CLI 可用"
ai_found=false
if command -v claude &> /dev/null; then
    ai_found=true
    echo -e " ${GREEN}(claude)${NC}"
elif command -v copilot &> /dev/null; then
    ai_found=true
    echo -e " ${GREEN}(copilot)${NC}"
elif command -v gemini &> /dev/null; then
    ai_found=true
    echo -e " ${GREEN}(gemini)${NC}"
fi

if [ "$ai_found" = true ]; then
    test_passed
else
    test_failed "未找到 AI CLI"
    echo "     請安裝: npm install -g @anthropic-ai/claude-cli"
fi

# ============================================
# 測試 4: 目錄結構
# ============================================

echo ""
echo -e "${YELLOW}[測試類別] 目錄結構${NC}"

run_test "Log 目錄存在"
if [ -d "$HOME/.ai-dev-logs" ]; then
    test_passed
else
    test_failed "目錄不存在"
    echo "     請執行: mkdir -p $HOME/.ai-dev-logs"
fi

run_test "Log 目錄可寫"
if [ -w "$HOME/.ai-dev-logs" ]; then
    test_passed
else
    test_failed "目錄無寫入權限"
fi

run_test "專案目錄存在"
if [ -d "$HOME/projects" ]; then
    test_passed
else
    test_failed "目錄不存在"
    echo "     請執行: mkdir -p $HOME/projects"
fi

# ============================================
# 測試 5: Cron 設定
# ============================================

echo ""
echo -e "${YELLOW}[測試類別] Cron 設定${NC}"

run_test "Cron job 已安裝"
if crontab -l 2>/dev/null | grep -q "ai-dev-loop.sh"; then
    test_passed
else
    test_failed "Cron job 未安裝"
    echo "     請執行: $SCRIPT_DIR/install-cron.sh"
fi

# ============================================
# 測試 6: 專案配置
# ============================================

echo ""
echo -e "${YELLOW}[測試類別] 專案配置${NC}"

run_test "環境配置檔存在"
if [ -f "$HOME/.ai-dev-env" ]; then
    test_passed
    
    # 如果存在，顯示當前專案
    source "$HOME/.ai-dev-env"
    if [ -n "$AI_DEV_PROJECT" ]; then
        echo "     目前專案: $AI_DEV_PROJECT"
    fi
else
    test_failed "未配置專案"
    echo "     請執行: $SCRIPT_DIR/ai-dev-control.sh start <project>"
fi

# ============================================
# 測試 7: 功能測試
# ============================================

echo ""
echo -e "${YELLOW}[測試類別] 功能測試${NC}"

run_test "控制腳本可執行"
if "$SCRIPT_DIR/ai-dev-control.sh" status &> /dev/null; then
    test_passed
else
    test_failed "執行失敗"
fi

run_test "專案配置可載入"
if source "$SCRIPT_DIR/project-configs.sh" 2>/dev/null; then
    test_passed
else
    test_failed "載入失敗"
fi

# ============================================
# 測試結果摘要
# ============================================

echo ""
echo -e "${BLUE}=========================================${NC}"
echo -e "${BLUE}   測試結果摘要${NC}"
echo -e "${BLUE}=========================================${NC}"
echo ""

success_rate=$((passed_tests * 100 / total_tests))

echo "  總測試數: $total_tests"
echo -e "  通過: ${GREEN}$passed_tests${NC}"
echo -e "  失敗: ${RED}$((total_tests - passed_tests))${NC}"
echo -e "  成功率: ${success_rate}%"
echo ""

if [ $passed_tests -eq $total_tests ]; then
    echo -e "${GREEN}🎉 所有測試通過！系統已準備就緒！${NC}"
    echo ""
    echo "下一步："
    echo "  1. 配置專案 Prompt: nano ~/projects/<project>/.ai-dev-prompt.txt"
    echo "  2. 立即執行測試: $SCRIPT_DIR/ai-dev-control.sh run"
    echo "  3. 查看狀態: $SCRIPT_DIR/ai-dev-control.sh status"
    exit 0
elif [ $success_rate -ge 70 ]; then
    echo -e "${YELLOW}⚠️  大部分測試通過，但有些項目需要修正${NC}"
    echo ""
    echo "請檢查上方失敗的測試項目"
    exit 1
else
    echo -e "${RED}❌ 測試失敗過多，請檢查安裝${NC}"
    echo ""
    echo "建議重新執行安裝："
    echo "  $SCRIPT_DIR/setup.sh"
    exit 1
fi
