#!/bin/bash

# Drama Ling 問題檢查工具
# 用途：檢查和統計問題狀態

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$(dirname "$SCRIPT_DIR")")"
ISSUES_FILE="$PROJECT_ROOT/docs/04_technical/issues-tracking.md"

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

echo -e "${BLUE}🔍 Drama Ling 問題追蹤檢查${NC}"
echo "=============================================="

if [ ! -f "$ISSUES_FILE" ]; then
    echo -e "${RED}❌ 問題追蹤檔案不存在: $ISSUES_FILE${NC}"
    exit 1
fi

# 檢查1: 問題總數統計
echo -e "${PURPLE}📊 1. 問題總數統計${NC}"
echo "----------------------------------------"

total_issues=$(grep -c "^## Issue #" "$ISSUES_FILE")
echo "總問題數量: $total_issues"

if [ $total_issues -eq 0 ]; then
    echo -e "${GREEN}🎉 沒有問題記錄${NC}"
    exit 0
fi

# 依優先級統計
critical_count=$(grep -c "🔴 Critical" "$ISSUES_FILE")
important_count=$(grep -c "🟡 Important" "$ISSUES_FILE")
normal_count=$(grep -c "🔵 Normal" "$ISSUES_FILE")
low_count=$(grep -c "🟢 Low" "$ISSUES_FILE")

echo "🔴 Critical: $critical_count"
echo "🟡 Important: $important_count"
echo "🔵 Normal: $normal_count"
echo "🟢 Low: $low_count"

echo ""

# 檢查2: 狀態統計
echo -e "${PURPLE}📋 2. 問題狀態統計${NC}"
echo "----------------------------------------"

open_count=$(grep -c "status:open" "$ISSUES_FILE")
progress_count=$(grep -c "status:in-progress" "$ISSUES_FILE")
resolved_count=$(grep -c "status:resolved" "$ISSUES_FILE")
closed_count=$(grep -c "status:closed" "$ISSUES_FILE")
blocked_count=$(grep -c "status:blocked" "$ISSUES_FILE")

echo "📂 Open: $open_count"
echo "⚡ In Progress: $progress_count"
echo "✅ Resolved: $resolved_count"
echo "🔒 Closed: $closed_count"
echo "🚫 Blocked: $blocked_count"

echo ""

# 檢查3: 模組分布
echo -e "${PURPLE}🏗️ 3. 問題模組分布${NC}"
echo "----------------------------------------"

api_count=$(grep -c "module:api" "$ISSUES_FILE")
auth_count=$(grep -c "module:auth" "$ISSUES_FILE")
ui_count=$(grep -c "module:ui" "$ISSUES_FILE")
db_count=$(grep -c "module:db" "$ISSUES_FILE")
docs_count=$(grep -c "module:docs" "$ISSUES_FILE")
learning_count=$(grep -c "module:learning" "$ISSUES_FILE")

echo "🔗 API: $api_count"
echo "🔐 Auth: $auth_count"
echo "🎨 UI: $ui_count"
echo "🗄️ Database: $db_count"
echo "📚 Docs: $docs_count"
echo "📖 Learning: $learning_count"

echo ""

# 檢查4: 緊急問題列表
if [ $critical_count -gt 0 ] || [ $blocked_count -gt 0 ]; then
    echo -e "${RED}🚨 4. 緊急問題列表${NC}"
    echo "----------------------------------------"
    
    if [ $critical_count -gt 0 ]; then
        echo -e "${RED}Critical 問題:${NC}"
        grep -A 1 "^## Issue #.*" "$ISSUES_FILE" | grep -B 1 "🔴 Critical" | grep "^## Issue #" | head -5
    fi
    
    if [ $blocked_count -gt 0 ]; then
        echo -e "${RED}被阻擋的問題:${NC}"
        grep -A 5 "^## Issue #.*" "$ISSUES_FILE" | grep -B 1 -A 3 "status:blocked" | grep "^## Issue #" | head -5
    fi
    
    echo ""
fi

# 檢查5: 逾期問題 (簡化版)
echo -e "${PURPLE}⏰ 5. 待辦問題檢查${NC}"
echo "----------------------------------------"

open_issues=$(grep -A 5 "^## Issue #.*" "$ISSUES_FILE" | grep -B 1 -A 3 "status:open" | grep "^## Issue #")
if [ -n "$open_issues" ]; then
    echo "未解決的問題:"
    echo "$open_issues" | head -5
    
    if [ $(echo "$open_issues" | wc -l) -gt 5 ]; then
        echo "... 還有 $(($(echo "$open_issues" | wc -l) - 5)) 個問題"
    fi
else
    echo -e "${GREEN}✅ 沒有待辦問題${NC}"
fi

echo ""

# 檢查6: 問題類型分析
echo -e "${PURPLE}🏷️ 6. 問題類型分析${NC}"
echo "----------------------------------------"

spec_count=$(grep -c "type:spec" "$ISSUES_FILE")
conflict_count=$(grep -c "type:conflict" "$ISSUES_FILE")
missing_count=$(grep -c "type:missing" "$ISSUES_FILE")
decision_count=$(grep -c "type:decision" "$ISSUES_FILE")
bug_count=$(grep -c "type:bug" "$ISSUES_FILE")

echo "📋 Spec: $spec_count"
echo "⚡ Conflict: $conflict_count"
echo "❓ Missing: $missing_count"
echo "🤔 Decision: $decision_count"
echo "🐛 Bug: $bug_count"

echo ""

# 生成健康度報告
echo -e "${BLUE}📈 7. 專案健康度評估${NC}"
echo "----------------------------------------"

# 計算健康度分數
health_score=100

# 扣分邏輯
if [ $critical_count -gt 0 ]; then
    health_score=$((health_score - critical_count * 20))
fi

if [ $blocked_count -gt 0 ]; then
    health_score=$((health_score - blocked_count * 15))
fi

if [ $open_count -gt 10 ]; then
    health_score=$((health_score - (open_count - 10) * 2))
fi

# 確保分數不為負數
if [ $health_score -lt 0 ]; then
    health_score=0
fi

echo "專案健康度分數: $health_score/100"

if [ $health_score -ge 80 ]; then
    echo -e "${GREEN}🎉 狀態: 優秀${NC}"
elif [ $health_score -ge 60 ]; then
    echo -e "${YELLOW}👍 狀態: 良好${NC}"
elif [ $health_score -ge 40 ]; then
    echo -e "${YELLOW}⚠️ 狀態: 需要關注${NC}"
else
    echo -e "${RED}🚨 狀態: 需要緊急處理${NC}"
fi

echo ""

# 提供改進建議
echo -e "${BLUE}💡 改進建議${NC}"
echo "----------------------------------------"

if [ $critical_count -gt 0 ]; then
    echo "• 優先解決 $critical_count 個 Critical 問題"
fi

if [ $blocked_count -gt 0 ]; then
    echo "• 移除 $blocked_count 個被阻擋的問題"
fi

if [ $open_count -gt 10 ]; then
    echo "• 處理過多的待辦問題 (建議控制在10個以內)"
fi

if [ $spec_count -gt 5 ]; then
    echo "• 規格問題較多，建議進行規格澄清會議"
fi

if [ $conflict_count -gt 0 ]; then
    echo "• 解決 $conflict_count 個衝突問題以避免開發延誤"
fi

echo ""
echo -e "${GREEN}✅ 問題檢查完成${NC}"