#!/bin/bash

echo "=== 应用 inkos 质量配置 ==="
echo "开始时间: $(date)"
echo ""

# 配置文件
QUALITY_CONFIG="/root/.openclaw/workspace/tomato-novel/inkos_quality_config.json"
BOOK_DIR="/root/.openclaw/workspace/tomato-novel/books/末日重生-开局囤货十亿物资"
BOOK_CONFIG="$BOOK_DIR/inkos_config.json"
BACKUP_DIR="$BOOK_DIR/config_backup_$(date +%Y%m%d_%H%M%S)"

# 1. 检查文件
echo "1. 检查配置文件..."
if [ ! -f "$QUALITY_CONFIG" ]; then
    echo "❌ 错误：质量配置文件不存在"
    exit 1
fi

if [ ! -d "$BOOK_DIR" ]; then
    echo "❌ 错误：书籍目录不存在"
    exit 1
fi

echo "✅ 配置文件: $QUALITY_CONFIG"
echo "✅ 书籍目录: $BOOK_DIR"
echo ""

# 2. 备份原始配置
echo "2. 备份原始配置..."
mkdir -p "$BACKUP_DIR"

if [ -f "$BOOK_CONFIG" ]; then
    cp "$BOOK_CONFIG" "$BACKUP_DIR/inkos_config_原始备份.json"
    echo "✅ 原始配置已备份: $BACKUP_DIR/inkos_config_原始备份.json"
else
    echo "⚠️  原始配置文件不存在，将创建新配置"
fi
echo ""

# 3. 应用质量配置
echo "3. 应用质量配置..."
cp "$QUALITY_CONFIG" "$BOOK_CONFIG"
echo "✅ 质量配置已应用: $BOOK_CONFIG"
echo ""

# 4. 验证配置
echo "4. 验证配置..."
if [ -f "$BOOK_CONFIG" ]; then
    CONFIG_SIZE=$(wc -c < "$BOOK_CONFIG")
    CONFIG_LINES=$(wc -l < "$BOOK_CONFIG")
    echo "✅ 配置验证通过"
    echo "   - 文件大小: $CONFIG_SIZE 字节"
    echo "   - 行数: $CONFIG_LINES 行"
else
    echo "❌ 错误：配置文件未创建"
    exit 1
fi
echo ""

# 5. 显示配置摘要
echo "5. 配置摘要:"
echo "   - 平台: tomato (番茄小说)"
echo "   - 目标读者: mobile_young_readers"
echo "   - 质量关卡: strict (严格)"
echo "   - 段落要求: 最小35字，短段比例<30%"
echo "   - 爽点要求: 每章至少3个爽点"
echo "   - 对话要求: 比例≥30%"
echo "   - 情绪弧线: 每章至少2次情绪变化"
echo "   - 自动修复: 启用段落合并、爽点增强、对话添加"
echo ""

# 6. 创建启动脚本
echo "6. 创建启动脚本..."
START_SCRIPT="/root/.openclaw/workspace/start_inkos_with_quality.sh"

cat > "$START_SCRIPT" << 'EOF'
#!/bin/bash
echo "=== 启动 inkos (质量增强版) ==="
echo "时间: $(date)"
echo ""

cd "/root/.openclaw/workspace/tomato-novel/books/末日重生-开局囤货十亿物资"
nohup inkos up --config inkos_config.json > /tmp/inkos_quality_startup.log 2>&1 &

sleep 5

if ps aux | grep "inkos up" | grep -v grep > /dev/null; then
    echo "✅ inkos 启动成功"
    echo "✅ PID: $(ps aux | grep 'inkos up' | grep -v grep | awk '{print $2}')"
    echo "✅ 日志: /tmp/inkos_quality_startup.log"
else
    echo "❌ inkos 启动失败"
    echo "查看日志: /tmp/inkos_quality_startup.log"
    exit 1
fi

echo ""
echo "=== 质量配置已启用 ==="
echo "📋 监控命令: bash /root/.openclaw/workspace/inkos_quality_monitor.sh"
echo "🛑 停止命令: pkill -f 'inkos up'"
echo "📊 质量检查: python3 /root/.openclaw/workspace/tomato-novel/scripts/simple_quality_check.py <章节文件>"
EOF

chmod +x "$START_SCRIPT"
echo "✅ 启动脚本创建: $START_SCRIPT"
echo ""

# 7. 创建监控脚本
echo "7. 创建质量监控脚本..."
MONITOR_SCRIPT="/root/.openclaw/workspace/inkos_quality_monitor.sh"

cat > "$MONITOR_SCRIPT" << 'EOF'
#!/bin/bash
echo "=== inkos 质量监控 ==="
echo "时间: $(date)"
echo ""

# 检查进程
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 未运行"
    echo "使用启动命令: bash /root/.openclaw/workspace/start_inkos_with_quality.sh"
    exit 1
fi

# 检查配置
CONFIG_FILE="/root/.openclaw/workspace/tomato-novel/books/末日重生-开局囤货十亿物资/inkos_config.json"
if [ -f "$CONFIG_FILE" ]; then
    echo "✅ 质量配置已加载"
else
    echo "❌ 质量配置未找到"
fi

# 检查最新章节
LATEST_CHAPTER=$(ls -t /root/.openclaw/workspace/tomato-novel/books/末日重生-开局囤货十亿物资/chapters/*.md 2>/dev/null | grep -v "_fixed\|_备份\|_修复\|backup" | head -1)
if [ -f "$LATEST_CHAPTER" ]; then
    CHAPTER_NAME=$(basename "$LATEST_CHAPTER")
    CHAPTER_SIZE=$(wc -c < "$LATEST_CHAPTER")
    echo "📖 最新章节: $CHAPTER_NAME ($CHAPTER_SIZE 字符)"
    
    # 检查质量
    echo ""
    echo "🔍 质量检查:"
    python3 /root/.openclaw/workspace/tomato-novel/scripts/simple_quality_check.py "$LATEST_CHAPTER" | tail -15
else
    echo "📖 未找到章节文件"
fi

# 检查日志
LOG_FILE="/tmp/inkos_quality_startup.log"
if [ -f "$LOG_FILE" ]; then
    echo ""
    echo "📝 最近日志:"
    tail -5 "$LOG_FILE"
fi
EOF

chmod +x "$MONITOR_SCRIPT"
echo "✅ 监控脚本创建: $MONITOR_SCRIPT"
echo ""

echo "=== 质量配置应用完成 ==="
echo "结束时间: $(date)"
echo ""
echo "🎯 下一步操作:"
echo "1. 启动 inkos: bash $START_SCRIPT"
echo "2. 监控状态: bash $MONITOR_SCRIPT"
echo "3. 检查质量: python3 /root/.openclaw/workspace/tomato-novel/scripts/simple_quality_check.py <章节文件>"
echo "4. 查看配置: cat $BOOK_CONFIG | head -50"
echo ""
echo "⚠️  注意：新配置将强制执行质量要求，不合格内容将被拒绝或自动修复"