#!/bin/bash

# inkos 状态报告脚本
# 直接通过命令检查状态，生成报告

PROJECT_DIR="/root/.openclaw/workspace/tomato-novel"
REPORT_FILE="/tmp/inkos-status-report.txt"

# 生成报告
generate_report() {
    echo "# 🦠 inkos 状态报告 $(date '+%Y-%m-%d %H:%M:%S')" > "$REPORT_FILE"
    echo "" >> "$REPORT_FILE"
    
    # 1. 检查守护进程
    echo "## 🔄 进程状态" >> "$REPORT_FILE"
    if pgrep -f "inkos up" > /dev/null; then
        DAEMON_PID=$(pgrep -f "inkos up")
        echo "✅ **inkos 守护进程运行中**" >> "$REPORT_FILE"
        echo "   PID: $DAEMON_PID" >> "$REPORT_FILE"
    else
        echo "❌ **inkos 守护进程已停止**" >> "$REPORT_FILE"
    fi
    echo "" >> "$REPORT_FILE"
    
    # 2. 检查项目状态
    echo "## 📚 项目状态" >> "$REPORT_FILE"
    if [ -d "$PROJECT_DIR" ]; then
        cd "$PROJECT_DIR"
        
        # 书籍列表
        BOOK_LIST=$(inkos book list 2>/dev/null || echo "无法获取书籍列表")
        echo "📖 **书籍**: $BOOK_LIST" >> "$REPORT_FILE"
        
        # 章节统计
        CHAPTER_COUNT=$(find books -name "*.md" -path "*/chapters/*" 2>/dev/null | wc -l)
        echo "📄 **章节数量**: $CHAPTER_COUNT" >> "$REPORT_FILE"
        
        # 显示章节详情
        if [ $CHAPTER_COUNT -gt 0 ]; then
            echo "" >> "$REPORT_FILE"
            echo "### 章节详情:" >> "$REPORT_FILE"
            find books -name "*.md" -path "*/chapters/*" 2>/dev/null | sort | while read chapter; do
                CHAPTER_NAME=$(basename "$chapter" .md)
                FILE_SIZE=$(stat -c%s "$chapter" 2>/dev/null)
                WORD_EST=$((FILE_SIZE / 3))  # 估算字数
                echo "  - $CHAPTER_NAME (约 ${WORD_EST}字)" >> "$REPORT_FILE"
            done
        fi
        
        # 检查最近修改时间
        echo "" >> "$REPORT_FILE"
        LATEST_CHAPTER=$(find books -name "*.md" -path "*/chapters/*" -printf "%T@ %p\n" 2>/dev/null | sort -nr | head -1 | cut -d' ' -f2-)
        if [ -n "$LATEST_CHAPTER" ]; then
            LATEST_TIME=$(stat -c "%y" "$LATEST_CHAPTER" 2>/dev/null | cut -d'.' -f1)
            echo "⏰ **最近更新**: $LATEST_TIME" >> "$REPORT_FILE"
        fi
    else
        echo "❌ **项目目录不存在**: $PROJECT_DIR" >> "$REPORT_FILE"
    fi
    echo "" >> "$REPORT_FILE"
    
    # 3. 检查日志
    echo "## 📝 系统日志" >> "$REPORT_FILE"
    if [ -f "/tmp/inkos-daemon.log" ]; then
        LOG_LINES=$(tail -3 "/tmp/inkos-daemon.log" 2>/dev/null)
        if [ -n "$LOG_LINES" ]; then
            echo "$LOG_LINES" >> "$REPORT_FILE"
        else
            echo "无最近日志" >> "$REPORT_FILE"
        fi
    else
        echo "日志文件不存在" >> "$REPORT_FILE"
    fi
    echo "" >> "$REPORT_FILE"
    
    # 4. 系统状态
    echo "## 🖥️  系统状态" >> "$REPORT_FILE"
    echo "⏱️  时间: $(date '+%Y-%m-%d %H:%M:%S %Z')" >> "$REPORT_FILE"
    echo "🏠 项目目录: $PROJECT_DIR" >> "$REPORT_FILE"
    echo "📅 监控开始时间: $(date '+%Y-%m-%d %H:%M:%S')" >> "$REPORT_FILE"
    
    # 5. 建议
    echo "" >> "$REPORT_FILE"
    echo "## 💡 建议" >> "$REPORT_FILE"
    if pgrep -f "inkos up" > /dev/null; then
        echo "✅ 系统运行正常，inkos 正在自动写作" >> "$REPORT_FILE"
    else
        echo "⚠️  inkos 守护进程已停止，建议重启" >> "$REPORT_FILE"
        echo "   运行: cd $PROJECT_DIR && nohup inkos up > /tmp/inkos-daemon.log 2>&1 &" >> "$REPORT_FILE"
    fi
}

# 主函数
main() {
    echo "正在生成 inkos 状态报告..."
    generate_report
    
    # 显示报告
    cat "$REPORT_FILE"
    
    # 通过飞书发送报告
    echo ""
    echo "报告已保存到: $REPORT_FILE"
    echo "可以通过飞书发送此报告"
}

# 执行
main