#!/bin/bash

echo "=== inkos 简单监控系统 ==="
echo "时间: $(date)"
echo ""

# 配置
PROJECT_DIR="/root/.openclaw/workspace/tomato-novel"
BOOK_ID="末日重生-开局囤货十亿物资"
CHAP_DIR="$PROJECT_DIR/books/$BOOK_ID/chapters"

# 获取初始章节数量
INITIAL_CHAP_COUNT=$(find "$CHAP_DIR" -name "*.md" -type f | grep -E "^[0-9]{4}_" | wc -l)

echo "📊 初始状态:"
echo "   - 章节数量: $INITIAL_CHAP_COUNT 章"
echo "   - 监控开始: $(date -Iseconds)"
echo ""

# 监控循环
MONITOR_INTERVAL=30  # 秒
MONITOR_CYCLES=60    # 30分钟

for ((i=1; i<=MONITOR_CYCLES; i++)); do

    # 1. 检查 inkos 状态

    if ps aux | grep "inkos up" | grep -v grep > /dev/null; then
        PID=$(ps aux | grep "inkos up" | grep -v grep | awk '{print $2}')
        STATUS="✅ 运行中 (PID: $PID)"
    else

        STATUS="❌ 未运行"

    fi
    
    # 2. 检查章节变化

    CURRENT_CHAP_COUNT=$(find "$CHAP_DIR" -name "*.md" -type f | grep -E "^[0-9]{4}_" | wc -l)
    NEW_CHAP_COUNT=$((CURRENT_CHAP_COUNT - INITIAL_CHAP_COUNT))
    
    # 3. 检查日志

    if [ -f /tmp/inkos_optimized.log ]; then

        LOG_TAIL=$(tail -2 /tmp/inkos_optimized.log 2>/dev/null | tr '\n' ' ')
    else

        LOG_TAIL="无日志"
    fi
    
    # 4. 显示状态
    echo "========================================"

    echo "监控周期 #$i - $(date +%H:%M:%S)"
    echo "========================================"
    echo ""
    echo "🖥️  inkos状态: $STATUS"

    echo "📚 章节统计:"
    echo "   - 初始数量: $INITIAL_CHAP_COUNT 章"

    echo "   - 当前数量: $CURRENT_CHAP_COUNT 章"
    echo "   - 新增章节: $NEW_CHAP_COUNT 章"
    echo ""

    # 如果有新章节，检查质量

    if [ $NEW_CHAP_COUNT -gt 0 ]; then

        echo "🎉 发现新章节!"
        echo "🔍 检查新章节:"
        
        # 找到最新的章节

        NEWEST_CHAP=$(find "$CHAP_DIR" -name "*.md" -type f -printf "%T@ %p\n" | sort -rn | head -1 | cut -d' ' -f2-)
        if [ -f "$NEWEST_CHAP" ]; then


            CHAP_NAME=$(basename "$NEWEST_CHAP")
            CHAP_SIZE=$(wc -c < "$NEWEST_CHAP" 2>/dev/null || echo 0)
            
            echo "   - 文件名: $CHAP_NAME"
            echo "   - 文件大小: $CHAP_SIZE 字符"
            echo ""
            
            # 质量检查

            echo "   🔍 质量检查:"
            python3 /root/.openclaw/workspace/tomato-novel/scripts/simple_quality_check.py "$NEWEST_CHAP" 2>/dev/null | \
                grep -E "段落分析|爽点分析|对话分析|发现问题|修复建议|章节号" | head -8
        fi
    fi
    
    # 5. 检查是否第19章产出



    if ls "$CHAP_DIR/0019_"*.md 2>/dev/null >/dev/null; then

        echo ""
        echo "🎉 第19章已产出!"

        CHAP_FILE=$(ls -t "$CHAP_DIR/0019_"*.md 2>/dev/null | head -1)
        CHAP_SIZE=$(wc -c < "$CHAP_FILE" 2>/dev/null || echo 0)
        
        echo "📊 章节信息:"
        echo "   - 文件名: $(basename "$CHAP_FILE")"

        echo "   - 文件大小: $CHAP_SIZE 字符"
        echo "   - 产出时间: $(stat -c %y "$CHAP_FILE" 2>/dev/null || echo "unknown")"
        echo ""
        
        # 详细质量检查

        echo "🔍 详细质量检查:"
        python3 /root/.openclaw/workspace/tomato-novel/scripts/simple_quality_check.py "$CHAP_FILE" 2>&1 | tail -30
        echo ""
        
        echo "✅ 监控目标达成!"
        echo "第19章已成功产出，请检查质量是否符合番茄小说标准"
        exit 0
    fi
    
    # 6. 显示日志

    echo "📝 最近日志: $LOG_TAIL"
    echo ""
    echo "⏳ 等待 ${MONITOR_INTERVAL}秒..."
    sleep $MONITOR_INTERVAL
    
    # 更新章节计数


    INITIAL_CHAP_COUNT=$CURRENT_CHAP_COUNT

done

echo ""
echo "========================================"
echo "监控结束 - 达到最长监控时间"
echo "时间: $(date)"
echo "========================================"
echo ""
echo "⚠️  注意：第19章尚未产出"
echo "📊 最后状态:"
echo "   - 章节数量: $CURRENT_CHAP_COUNT 章"
echo "   - inkos状态: $STATUS"
echo "   - 最近日志: $LOG_TAIL"
echo ""
echo "🎯 建议:"
echo "1. 检查 inkos 日志: /tmp/inkos_optimized.log"
echo "2. 可能需要手动干预"
echo "3. 检查 spot-fix 修复状态"
echo "4. 考虑重启 inkos"