#!/bin/bash

echo "=== inkos 综合监控系统 ==="
echo "时间: $(date)"
echo "监控范围: 产出监控 + 质量变化监控 + 系统状态监控"
echo ""

# 配置
PROJECT_DIR="/root/.openclaw/workspace/tomato-novel"
BOOK_ID="末日重生-开局囤货十亿物资"
CHAP_DIR="$PROJECT_DIR/books/$BOOK_ID/chapters"
LOG_FILE="/tmp/inkos_optimized.log"
QUALITY_SCRIPT="/root/.openclaw/workspace/tomato-novel/scripts/simple_quality_check.py"
MONITOR_INTERVAL=30  # 秒
MAX_MONITOR_MINUTES=30

# 创建监控目录
MONITOR_DIR="/root/.openclaw/workspace/inkos_monitor_data"
mkdir -p "$MONITOR_DIR"

# 初始化状态文件
STATE_FILE="$MONITOR_DIR/monitor_state.json"
if [ ! -f "$STATE_FILE" ]; then
    cat > "$STATE_FILE" << EOF
{
  "last_chapter_count": 0,
  "last_audit_pass_rate": 0,
  "last_quality_score": 0,
  "chapters_checked": [],
  "monitor_start_time": "$(date -Iseconds)",
  "quality_reports": {}
}
EOF
fi

# 函数：获取当前状态
get_current_state() {
    echo "📊 获取当前状态..."
    
    # 1. inkos 进程状态
    if ps aux | grep "inkos up" | grep -v grep > /dev/null; then
        INKOS_PID=$(ps aux | grep "inkos up" | grep -v grep | awk '{print $2}')
        INKOS_STATUS="running"
    else
        INKOS_PID=""
        INKOS_STATUS="stopped"
    fi
    
    # 2. 章节数量
    CHAPTER_COUNT=$(find "$CHAP_DIR" -name "*.md" -type f | grep -E "^[0-9]{4}_" | wc -l)
    
    # 3. 最新章节
    LATEST_CHAPTER=$(find "$CHAP_DIR" -name "*.md" -type f -printf "%T@ %p\n" | sort -rn | head -1 | cut -d' ' -f2-)
    if [ -n "$LATEST_CHAPTER" ]; then
        LATEST_NAME=$(basename "$LATEST_CHAPTER")
        LATEST_SIZE=$(wc -c < "$LATEST_CHAPTER" 2>/dev/null || echo 0)
        LATEST_TIME=$(stat -c %y "$LATEST_CHAPTER" 2>/dev/null || echo "unknown")
    else
        LATEST_NAME="none"
        LATEST_SIZE=0
        LATEST_TIME="unknown"
    fi
    
    # 4. 日志状态
    if [ -f "$LOG_FILE" ]; then
        LOG_LINES=$(wc -l < "$LOG_FILE")
        LOG_TAIL=$(tail -3 "$LOG_FILE" | tr '\n' ' ')
    else
        LOG_LINES=0
        LOG_TAIL="no log file"
    fi
    
    # 5. 质量评估（定期执行，避免每次调用）
    QUALITY_DATA="{}"
    if [ $(( $(date +%s) % 300 )) -lt 10 ]; then  # 每5分钟执行一次
        echo "   🔍 执行定期质量评估..."
        cd "$PROJECT_DIR"
        inkos eval "$BOOK_ID" --json > /tmp/inkos_eval_latest.json 2>/dev/null
        if [ -f /tmp/inkos_eval_latest.json ]; then
            QUALITY_DATA=$(cat /tmp/inkos_eval_latest.json)
        fi
    fi
    
    # 返回状态
    cat << EOF
{
  "timestamp": "$(date -Iseconds)",
  "inkos_status": "$INKOS_STATUS",
  "inkos_pid": "$INKOS_PID",
  "chapter_count": $CHAPTER_COUNT,
  "latest_chapter": {
    "name": "$LATEST_NAME",
    "size": $LATEST_SIZE,
    "time": "$LATEST_TIME"
  },
  "log_status": {
    "lines": $LOG_LINES,
    "tail": "$LOG_TAIL"
  },
  "quality_data": $QUALITY_DATA
}
EOF
}

# 函数：检查新章节
check_new_chapters() {
    echo "🔍 检查新章节..."
    
    # 获取当前章节列表
    CURRENT_CHAPS=$(find "$CHAP_DIR" -name "*.md" -type f -printf "%f\n" | sort)
    LAST_CHAPS=$(jq -r '.chapters_checked[]' "$STATE_FILE" 2>/dev/null || echo "")
    
    # 比较差异
    NEW_CHAPS=$(comm -13 <(echo "$LAST_CHAPS" | sort) <(echo "$CURRENT_CHAPS" | sort))
    
    if [ -n "$NEW_CHAPS" ]; then
        echo "🎉 发现新章节:"
        echo "$NEW_CHAPS" | while read -r chap; do
            echo "   📄 $chap"
            
            # 检查新章节质量
            CHAPTER_FILE="$CHAP_DIR/$chap"
            if [ -f "$CHAPTER_FILE" ]; then
                echo "   🔍 执行质量检查..."
                QUALITY_REPORT=$(python3 "$QUALITY_SCRIPT" "$CHAPTER_FILE" 2>/dev/null | grep -E "段落分析|爽点分析|对话分析|发现问题|修复建议")
                echo "$QUALITY_REPORT" | sed 's/^/      /'
                
                # 保存质量报告
                REPORT_FILE="$MONITOR_DIR/${chap}_quality_$(date +%Y%m%d_%H%M%S).txt"
                python3 "$QUALITY_SCRIPT" "$CHAPTER_FILE" > "$REPORT_FILE" 2>&1
                echo "   📊 报告保存: $(basename "$REPORT_FILE")"
            fi
        done
        
        # 更新状态文件
        jq --arg newchaps "$NEW_CHAPS" \
           '.chapters_checked = (.chapters_checked + ($newchaps | split("\n") | map(select(. != ""))))' \
           "$STATE_FILE" > "$STATE_FILE.tmp" && mv "$STATE_FILE.tmp" "$STATE_FILE"
        
        return 1  # 有新章节
    else
        echo "   📝 无新章节"
        return 0  # 无新章节
    fi
}

# 函数：监控质量变化
monitor_quality_changes() {
    echo "📈 监控质量变化..."
    
    # 获取最新的质量评估
    if [ -f /tmp/inkos_eval_latest.json ]; then
        CURRENT_EVAL=$(cat /tmp/inkos_eval_latest.json)
        
        # 提取关键指标
        CURRENT_SCORE=$(echo "$CURRENT_EVAL" | jq -r '.qualityScore // 0')
        CURRENT_PASS_RATE=$(echo "$CURRENT_EVAL" | jq -r '.auditPassRate // 0')
        FAILED_COUNT=$(echo "$CURRENT_EVAL" | jq -r '.chapters[] | select(.status == "audit-failed") | .number' | wc -l)
        
        # 获取之前的状态
        LAST_SCORE=$(jq -r '.last_quality_score' "$STATE_FILE")
        LAST_PASS_RATE=$(jq -r '.last_audit_pass_rate' "$STATE_FILE")
        
        # 检查变化
        SCORE_CHANGE=$(echo "$CURRENT_SCORE - $LAST_SCORE" | bc)
        PASS_RATE_CHANGE=$(echo "$CURRENT_PASS_RATE - $LAST_PASS_RATE" | bc)
        
        if [ $(echo "$SCORE_CHANGE != 0" | bc) -eq 1 ] || [ $(echo "$PASS_RATE_CHANGE != 0" | bc) -eq 1 ]; then
            echo "   📊 质量指标变化:"
            echo "      质量评分: $LAST_SCORE → $CURRENT_SCORE (${SCORE_CHANGE:+$SCORE_CHANGE})"
            echo "      审核通过率: $LAST_PASS_RATE% → $CURRENT_PASS_RATE% (${PASS_RATE_CHANGE:+$PASS_RATE_CHANGE}%)"
            echo "      审核失败章节: $FAILED_COUNT 章"
            
            # 更新状态
            jq --argjson score "$CURRENT_SCORE" \
               --argjson rate "$CURRENT_PASS_RATE" \
               '.last_quality_score = $score | .last_audit_pass_rate = $rate' \
               "$STATE_FILE" > "$STATE_FILE.tmp" && mv "$STATE_FILE.tmp" "$STATE_FILE"
        else
            echo "   📊 质量指标稳定:"
            echo "      质量评分: $CURRENT_SCORE"
            echo "      审核通过率: $CURRENT_PASS_RATE%"
            echo "      审核失败章节: $FAILED_COUNT 章"
        fi
    else
        echo "   ⚠️  暂无质量评估数据"
    fi
}

# 函数：监控系统状态
monitor_system_status() {
    echo "🖥️  监控系统状态..."
    
    # CPU 和内存使用
    if ps aux | grep "inkos up" | grep -v grep > /dev/null; then
        PID=$(ps aux | grep "inkos up" | grep -v grep | awk '{print $2}')
        CPU_USAGE=$(ps -p "$PID" -o %cpu --no-headers 2>/dev/null || echo "0")
        MEM_USAGE=$(ps -p "$PID" -o %mem --no-headers 2>/dev/null || echo "0")
        echo "   🔧 inkos 进程状态:"
        echo "      PID: $PID"
        echo "      CPU使用: ${CPU_USAGE}%"
        echo "     内存使用: ${MEM_USAGE}%"
    else
        echo "   ❌ inkos 未运行"
    fi
    
    # 磁盘空间
    DISK_USAGE=$(df -h "$PROJECT_DIR" | tail -1 | awk '{print $5}')
    echo "   💾 磁盘使用: $DISK_USAGE"
}

# 主监控循环
echo "🔄 开始综合监控..."
echo "监控间隔: ${MONITOR_INTERVAL}秒"
echo "最长监控: ${MAX_MONITOR_MINUTES}分钟"
echo ""

for ((i=1; i<=$((MAX_MONITOR_MINUTES*60/MONITOR_INTERVAL)); i++)); do
    echo "========================================"
    echo "监控周期 #$i - $(date +%H:%M:%S)"
    echo "========================================"
    
    # 1. 检查系统状态
    monitor_system_status
    echo ""
    
    # 2. 检查新章节
    if check_new_chapters; then
        echo "   📝 无新章节产出"
    fi
    echo ""
    
    # 3. 监控质量变化（每3个周期执行一次）
    if [ $((i % 3)) -eq 0 ]; then
        monitor_quality_changes
        echo ""
    fi
    
    # 4. 显示当前状态摘要
    echo "📋 状态摘要:"
    CURRENT_STATE=$(get_current_state)
    echo "$CURRENT_STATE" | jq -r '
        "   章节数量: \(.chapter_count) 章",
        "   最新章节: \(.latest_chapter.name) (\(.latest_chapter.size) 字符)",
        "   inkos状态: \(.inkos_status) (PID: \(.inkos_pid))",
        "   日志状态: \(.log_status.tail)"
    '
    
    # 保存监控快照
    SNAPSHOT_FILE="$MONITOR_DIR/snapshot_$(date +%Y%m%d_%H%M%S).json"
    echo "$CURRENT_STATE" > "$SNAPSHOT_FILE"
    
    echo ""
    echo "⏳ 等待 ${MONITOR_INTERVAL}秒..."
    sleep $MONITOR_INTERVAL
done

echo ""
echo "========================================"
echo "监控结束 - 达到最长监控时间"
echo "时间: $(date)"
echo "========================================"
echo ""
echo "📁 监控数据保存位置: $MONITOR_DIR"
echo "📊 监控报告:"
find "$MONITOR_DIR" -name "*.json" -o -name "*quality_*.txt" | sort | tail -5 | while read -r file; do
    echo "   📄 $(basename "$file")"
done
echo ""
echo "🎯 建议:"
echo "1. 检查新章节质量是否符合番茄小说标准"
echo "2. 观察审核通过率是否提升"
echo "3. 根据监控数据调整写作参数"
echo "4. 准备符合标准的章节投稿"