#!/bin/bash

# 文檔品質檢查腳本
echo "🔍 文檔品質檢查開始..."
echo "====================================="

# 檢查文檔完整性
echo "📄 1. 文檔完整性檢查"
echo "-------------------------------------"

# 必要文檔清單
required_docs=(
    "docs/01_requirement/requirements.md"
    "docs/01_requirement/system_structure_design.json"
    "docs/04_technical/user-flow-specification.md"
    "docs/04_technical/tech-stack-decision.md"
    "docs/04_technical/api-specifications.md"
    "docs/04_technical/database-schema.md"
    "docs/02_design/ui-ux-guidelines.md"
    "docs/02_design/business-logic-rules.md"
)

missing_docs=0
for doc in "${required_docs[@]}"; do
    if [ -f "$doc" ]; then
        echo "✅ $doc"
    else
        echo "❌ $doc (缺失)"
        missing_docs=$((missing_docs + 1))
    fi
done

echo "文檔完整性: $((${#required_docs[@]} - missing_docs))/${#required_docs[@]}"
echo ""

# 檢查文檔大小和內容
echo "📏 2. 文檔內容品質檢查"
echo "-------------------------------------"

for doc in "${required_docs[@]}"; do
    if [ -f "$doc" ]; then
        size=$(wc -c < "$doc")
        lines=$(wc -l < "$doc")
        words=$(wc -w < "$doc")
        
        if [ $size -lt 1000 ]; then
            echo "⚠️ $doc: 內容過少 (${size} bytes, ${lines} lines)"
        elif [ $size -gt 100000 ]; then
            echo "⚠️ $doc: 內容過多 (${size} bytes, ${lines} lines)"
        else
            echo "✅ $doc: 內容適中 (${words} words, ${lines} lines)"
        fi
    fi
done
echo ""

# 檢查Markdown格式
echo "📝 3. Markdown格式檢查"
echo "-------------------------------------"

find docs/ -name "*.md" -type f | while read -r file; do
    # 檢查標題結構
    h1_count=$(grep -c "^# " "$file" 2>/dev/null || echo 0)
    h2_count=$(grep -c "^## " "$file" 2>/dev/null || echo 0)
    
    if [ "$h1_count" -eq 0 ]; then
        echo "⚠️ $file: 缺少主標題 (H1)"
    elif [ "$h1_count" -gt 1 ]; then
        echo "⚠️ $file: 多個主標題 ($h1_count 個H1)"
    else
        echo "✅ $file: 標題結構良好"
    fi
done
echo ""

# 檢查TODO和FIXME
echo "📋 4. TODO和FIXME檢查"
echo "-------------------------------------"

todo_count=0
fixme_count=0

find docs/ -name "*.md" -type f | while read -r file; do
    todos=$(grep -in "TODO\|FIXME" "$file" 2>/dev/null || true)
    if [ -n "$todos" ]; then
        echo "📌 $file:"
        echo "$todos" | head -5
        echo ""
    fi
done
echo ""

# 檢查連結有效性
echo "🔗 5. 內部連結檢查"
echo "-------------------------------------"

find docs/ -name "*.md" -type f | while read -r file; do
    # 檢查內部連結 [text](path)
    grep -o '\[.*\](docs/[^)]*\|[^)]*\.md)' "$file" 2>/dev/null | while read -r link; do
        # 提取路徑
        path=$(echo "$link" | sed 's/.*(\([^)]*\)).*/\1/')
        if [[ "$path" == docs/* ]] || [[ "$path" == *.md ]]; then
            if [ ! -f "$path" ]; then
                echo "❌ $file: 無效連結 -> $path"
            fi
        fi
    done
done
echo ""

# 生成總結
echo "📊 文檔品質總結"
echo "====================================="

total_docs=${#required_docs[@]}
existing_docs=$((total_docs - missing_docs))
completeness=$((existing_docs * 100 / total_docs))

echo "文檔完整度: $completeness% ($existing_docs/$total_docs)"

if [ $completeness -ge 90 ]; then
    echo "✅ 品質評級: 優秀"
elif [ $completeness -ge 80 ]; then
    echo "✅ 品質評級: 良好"
elif [ $completeness -ge 70 ]; then
    echo "⚠️ 品質評級: 尚可"
else
    echo "❌ 品質評級: 需要改進"
fi

echo "檢查完成時間: $(date)"