#!/bin/bash

echo "=== 第19章最终检查 ==="
echo "时间: $(date)"
echo ""

CHAP_DIR="/root/.openclaw/workspace/tomato-novel/books/末日重生-开局囤货十亿物资/chapters"
LOG_FILE="/tmp/inkos_optimized.log"

# 检查第19章是否产出
echo "🔍 检查第19章产出状态..."
CHAPTER_FILE=$(ls -t "$CHAP_DIR/0019_"*.md 2>/dev/null | head -1)

if [ -n "$CHAPTER_FILE" ]; then
    CHAPTER_NAME=$(basename "$CHAPTER_FILE")
    CHAPTER_SIZE=$(wc -c < "$CHAPTER_FILE")
    
    echo "🎉 第19章已产出！"
    echo "📊 产出信息:"
    echo "   - 文件名: $CHAPTER_NAME"
    echo "   - 文件大小: $CHAPTER_SIZE 字符"
    echo "   - 产出时间: $(stat -c %y "$CHAPTER_FILE")"
    echo ""
    
    # 质量检查
    echo "🔍 质量检查:"
    python3 /root/.openclaw/workspace/tomato-novel/scripts/simple_quality_check.py "$CHAPTER_FILE" | grep -E "段落分析|爽点分析|对话分析|发现问题|修复建议" | head -20
    
    # 检查是否通过 spot-fix
    echo ""
    echo "🔧 spot-fix 修复验证:"
    if grep -q "深吸一口气" "$CHAPTER_FILE" 2>/dev/null; then
        echo "   ⚠️  可能仍有高疲劳词"
    else
        echo "   ✅ 高疲劳词已修复"
    fi
    
    if grep -q "——" "$CHAPTER_FILE" 2>/dev/null; then
        echo "   ⚠️  可能仍有破折号"
    else
        echo "   ✅ 破折号已修复"
    fi
else
    echo "⏳ 第19章尚未产出"
    echo ""
    echo "📝 最新日志:"
    tail -10 "$LOG_FILE" 2>/dev/null || echo "   无日志记录"
    
    # 检查 inkos 状态
    echo ""
    echo "🔄 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}')
        echo "   ✅ inkos 运行中 (PID: $PID)"
    else
        echo "   ❌ inkos 未运行"
    fi
fi

echo ""
echo "=== 检查完成 ==="