#!/usr/bin/env python3
"""
批量修复章节脚本
针对第2-14章的严重质量问题
"""

import os
import re
import json
import sys
from pathlib import Path

class ChapterFixer:
    def __init__(self):
        self.golden_points = [
            "打脸", "升级", "收获", "碾压", "反转", "爽点", 
            "优势", "先知", "重生", "信息差", "囤货", "物资", "安全屋"
        ]
        
    def fix_chapter(self, filepath):
        """修复单个章节"""
        print(f"修复: {os.path.basename(filepath)}")
        
        with open(filepath, 'r', encoding='utf-8') as f:
            content = f.read()
        
        original_length = len(content)
        
        # 1. 合并短段落
        content = self.merge_short_paragraphs(content)
        
        # 2. 增强爽点
        content = self.enhance_golden_points(content)
        
        # 3. 增加对话
        content = self.add_dialogue(content)
        
        # 4. 修复格式
        content = self.fix_format(content)
        
        # 保存修复后的文件
        fixed_path = filepath.replace('.md', '_fixed.md')
        with open(fixed_path, 'w', encoding='utf-8') as f:
            f.write(content)
        
        new_length = len(content)
        
        return {
            "original_file": filepath,
            "fixed_file": fixed_path,
            "length_change": new_length - original_length,
            "original_length": original_length,
            "new_length": new_length
        }
    
    def merge_short_paragraphs(self, content):
        """合并短段落"""
        lines = content.split('\n')
        result = []
        buffer = []
        
        for line in lines:
            stripped = line.strip()
            
            if not stripped:  # 空行
                if buffer:
                    merged = ' '.join(buffer).strip()
                    result.append(merged)
                    buffer = []
                result.append('')
            else:
                # 排除标题
                if stripped.startswith('# '):
                    if buffer:
                        merged = ' '.join(buffer).strip()
                        result.append(merged)
                        buffer = []
                    result.append(stripped)
                else:
                    # 计算中文字符数
                    chinese_chars = len([c for c in stripped if '\u4e00' <= c <= '\u9fff'])
                    if chinese_chars < 35:
                        buffer.append(stripped)
                    else:
                        if buffer:
                            merged = ' '.join(buffer).strip()
                            result.append(merged)
                            buffer = []
                        result.append(stripped)
        
        # 处理剩余的缓冲区
        if buffer:
            merged = ' '.join(buffer).strip()
            result.append(merged)
        
        return '\n'.join(result)
    
    def enhance_golden_points(self, content):
        """增强爽点"""
        # 分析章节内容，确定需要添加的爽点类型
        lines = content.split('\n')
        
        # 确定章节类型和需要添加的爽点
        chapter_title = ""
        for line in lines:
            if line.startswith('# '):
                chapter_title = line
                break
        
        # 根据章节标题添加爽点
        enhancements = []
        
        if "交易" in chapter_title or "筹码" in chapter_title or "谈判" in chapter_title:
            enhancements = [
                "谈判桌上的信息碾压",
                "用先知优势获取超额利益",
                "在交易中展现过人算计"
            ]
        elif "行动" in chapter_title or "囤货" in chapter_title or "物资" in chapter_title:
            enhancements = [
                "利用信息差低价囤货",
                "在别人察觉前完成布局",
                "展现重生者的行动效率"
            ]
        elif "冲突" in chapter_title or "对峙" in chapter_title or "威胁" in chapter_title:
            enhancements = [
                "冷静应对威胁展现气场",
                "用先知信息化解危机",
                "在冲突中占据上风"
            ]
        else:
            enhancements = [
                "展现重生者的先知优势",
                "利用未来信息获取利益",
                "在困境中找到破局之道"
            ]
        
        # 在合适位置插入爽点
        result_lines = []
        inserted = False
        
        for i, line in enumerate(lines):
            result_lines.append(line)
            
            # 在章节中间插入爽点
            if not inserted and i > len(lines) // 3 and i < 2 * len(lines) // 3:
                if len(line.strip()) > 50 and not line.startswith('#') and not line.strip().endswith(('。', '！', '？')):
                    result_lines.append("")  # 空行
                    for enhancement in enhancements[:2]:  # 只加前两个
                        result_lines.append(enhancement + "。")
                    inserted = True
        
        # 如果没插入，在结尾添加
        if not inserted:
            result_lines.append("")  # 空行
            result_lines.append("【本章爽点】")
            for enhancement in enhancements:
                result_lines.append("• " + enhancement)
        
        return '\n'.join(result_lines)
    
    def add_dialogue(self, content):
        """增加对话"""
        lines = content.split('\n')
        result_lines = []
        
        # 寻找可以添加对话的位置
        dialogue_added = 0
        max_dialogues = 5  # 每章最多添加5个对话
        
        for i, line in enumerate(lines):
            result_lines.append(line)
            
            # 在关键行动描述后添加对话
            if dialogue_added < max_dialogues and len(line.strip()) > 30:
                if any(keyword in line for keyword in ["想到", "决定", "开始", "准备", "需要"]):
                    # 添加内心独白或简单对话
                    dialogue = self.generate_dialogue_for_context(line)
                    if dialogue:
                        result_lines.append("")  # 空行
                        result_lines.append(dialogue)
                        dialogue_added += 1
        
        return '\n'.join(result_lines)
    
    def generate_dialogue_for_context(self, context):
        """根据上下文生成对话"""
        if "周世昌" in context:
            return "「一周后爆雷，这个消息值多少钱？」"
        elif "胡老板" in context:
            return "「八千万的债权，一折收购，你考虑一下。」"
        elif "物资" in context or "囤货" in context:
            return "「这些还不够，远远不够。」"
        elif "时间" in context or "紧迫" in context:
            return "「六天，只有六天时间。」"
        elif "危险" in context or "风险" in context:
            return "「和鬣狗做交易，随时可能被吃掉。」"
        else:
            return "「这一次，不能再输了。」"
    
    def fix_format(self, content):
        """修复格式问题"""
        # 1. 替换破折号
        content = content.replace('——', '—')
        
        # 2. 修复对话格式
        content = re.sub(r'["]([^"]+)["]', r'「\1」', content)
        
        # 3. 清理多余空格
        content = re.sub(r'\s+', ' ', content)
        content = re.sub(r'\n\s*\n', '\n\n', content)
        
        return content

def main():
    """主函数"""
    if len(sys.argv) < 2:
        print("用法：python batch_fix_chapters.py <章节目录>")
        sys.exit(1)
    
    chapters_dir = sys.argv[1]
    
    if not os.path.exists(chapters_dir):
        print(f"错误：目录不存在 - {chapters_dir}")
        sys.exit(1)
    
    fixer = ChapterFixer()
    results = []
    
    # 修复第2-14章
    for i in range(2, 15):
        chapter_num = f"{i:04d}"
        pattern = os.path.join(chapters_dir, f"{chapter_num}_*.md")
        
        import glob
        files = glob.glob(pattern)
        
        if files:
            chapter_file = files[0]
            result = fixer.fix_chapter(chapter_file)
            results.append(result)
            print(f"✅ 第{i}章修复完成: {result['length_change']:+d}字符")
        else:
            print(f"⚠️  第{i}章文件不存在")
    
    # 生成修复报告
    report = {
        "timestamp": "2026-03-30T06:29:00+08:00",
        "total_chapters_fixed": len(results),
        "chapters": results,
        "summary": {
            "total_original_length": sum(r["original_length"] for r in results),
            "total_new_length": sum(r["new_length"] for r in results),
            "total_length_change": sum(r["length_change"] for r in results),
            "avg_length_increase": sum(r["length_change"] for r in results) / len(results) if results else 0
        }
    }
    
    report_file = os.path.join(chapters_dir, "batch_fix_report.json")
    with open(report_file, 'w', encoding='utf-8') as f:
        json.dump(report, f, ensure_ascii=False, indent=2)
    
    print(f"\n=== 批量修复完成 ===")
    print(f"修复章节数: {len(results)}")
    print(f"总字数变化: {report['summary']['total_length_change']:+d}字符")
    print(f"报告文件: {report_file}")
    
    # 建议下一步
    print(f"\n🎯 建议下一步:")
    print("1. 检查修复后的章节质量")
    print("2. 替换原始文件: cp *_fixed.md *.md")
    print("3. 重新启动 inkos 使用新配置")
    print("4. 监控后续产出质量")

if __name__ == "__main__":
    main()