#!/bin/bash

# Drama Ling 報告檢查工具
# 用途：檢查報告狀態和統計

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

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

echo -e "${BLUE}📊 Drama Ling 報告狀態檢查${NC}"
echo "=============================================="

# 檢查報告目錄是否存在
if [ ! -d "$REPORTS_DIR" ]; then
    echo -e "${RED}❌ 報告目錄不存在: $REPORTS_DIR${NC}"
    exit 1
fi

# 統計報告數量
echo -e "${PURPLE}📈 1. 報告數量統計${NC}"
echo "----------------------------------------"

analysis_count=$(find "$REPORTS_DIR/analysis" -name "*.md" 2>/dev/null | wc -l | tr -d ' ')
decisions_count=$(find "$REPORTS_DIR/decisions" -name "*.md" 2>/dev/null | wc -l | tr -d ' ')
archive_count=$(find "$REPORTS_DIR/archive" -name "*.md" 2>/dev/null | wc -l | tr -d ' ')

echo "📊 分析報告: $analysis_count 份"
echo "📋 決策記錄: $decisions_count 份"
echo "📦 已歸檔: $archive_count 份"
echo "📄 總計: $((analysis_count + decisions_count + archive_count)) 份報告"

echo ""

# 最近的報告
echo -e "${PURPLE}📅 2. 最近的報告${NC}"
echo "----------------------------------------"

echo -e "${BLUE}分析報告:${NC}"
if [ $analysis_count -gt 0 ]; then
    find "$REPORTS_DIR/analysis" -name "*.md" -exec basename {} \; 2>/dev/null | sort -r | head -5
else
    echo "  無分析報告"
fi

echo ""
echo -e "${BLUE}決策記錄:${NC}"
if [ $decisions_count -gt 0 ]; then
    find "$REPORTS_DIR/decisions" -name "*.md" -exec basename {} \; 2>/dev/null | sort -r | head -5
else
    echo "  無決策記錄"
fi

echo ""

# 與問題系統的整合狀態
echo -e "${PURPLE}🔗 3. 問題系統整合狀態${NC}"
echo "----------------------------------------"

if [ -f "$SCRIPT_DIR/ISSUES.md" ]; then
    linked_reports=$(grep -c "📊.*報告.*\.md" "$SCRIPT_DIR/ISSUES.md")
    echo "已連結到問題的報告: $linked_reports 份"
    
    # 檢查是否有報告未連結到問題系統
    total_reports=$((analysis_count + decisions_count))
    if [ $total_reports -gt $linked_reports ]; then
        echo -e "${YELLOW}⚠️ 有 $((total_reports - linked_reports)) 份報告尚未連結到問題系統${NC}"
    else
        echo -e "${GREEN}✅ 所有報告都已連結到問題系統${NC}"
    fi
else
    echo -e "${RED}❌ 找不到 ISSUES.md 檔案${NC}"
fi

echo ""

# 報告命名規範檢查
echo -e "${PURPLE}📝 4. 命名規範檢查${NC}"
echo "----------------------------------------"

naming_issues=0

# 檢查分析報告命名
if [ $analysis_count -gt 0 ]; then
    echo "檢查分析報告命名規範..."
    find "$REPORTS_DIR/analysis" -name "*.md" 2>/dev/null | while read -r file; do
        filename=$(basename "$file")
        if [[ ! $filename =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}_.*-analysis\.md$ ]]; then
            echo -e "${YELLOW}⚠️ 命名不規範: $filename${NC}"
            naming_issues=$((naming_issues + 1))
        fi
    done
fi

# 檢查決策記錄命名
if [ $decisions_count -gt 0 ]; then
    echo "檢查決策記錄命名規範..."
    find "$REPORTS_DIR/decisions" -name "*.md" 2>/dev/null | while read -r file; do
        filename=$(basename "$file")
        if [[ ! $filename =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}_.*-decision\.md$ ]]; then
            echo -e "${YELLOW}⚠️ 命名不規範: $filename${NC}"
            naming_issues=$((naming_issues + 1))
        fi
    done
fi

if [ $naming_issues -eq 0 ]; then
    echo -e "${GREEN}✅ 所有報告命名規範正確${NC}"
fi

echo ""

# 報告健康度評估
echo -e "${PURPLE}🏥 5. 報告系統健康度${NC}"
echo "----------------------------------------"

health_score=100

# 扣分邏輯
if [ $total_reports -eq 0 ]; then
    health_score=$((health_score - 50))
    echo -e "${RED}📊 無報告: -50分${NC}"
elif [ $total_reports -lt 3 ]; then
    health_score=$((health_score - 20))
    echo -e "${YELLOW}📊 報告較少: -20分${NC}"
fi

if [ $total_reports -gt $linked_reports ]; then
    missing_links=$((total_reports - linked_reports))
    deduction=$((missing_links * 10))
    health_score=$((health_score - deduction))
    echo -e "${YELLOW}🔗 未連結報告: -${deduction}分${NC}"
fi

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

echo ""
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 [ $total_reports -eq 0 ]; then
    echo "• 開始建立第一份分析報告"
elif [ $total_reports -lt 3 ]; then
    echo "• 增加報告數量，建立完整的問題分析記錄"
fi

if [ $total_reports -gt $linked_reports ]; then
    echo "• 將 $((total_reports - linked_reports)) 份未連結報告整合到問題系統"
fi

if [ $naming_issues -gt 0 ]; then
    echo "• 修正 $naming_issues 個檔案的命名規範"
fi

if [ $analysis_count -gt 0 ] && [ $decisions_count -eq 0 ]; then
    echo "• 考慮建立技術決策記錄，記錄重要決策過程"
fi

echo ""
echo -e "${GREEN}✅ 報告檢查完成${NC}"