#!/usr/bin/env python3
import json
import os
from datetime import datetime

# 读取 index.json 文件
index_path = "books/末日重生-开局囤货十亿物资/chapters/index.json"
with open(index_path, 'r', encoding='utf-8') as f:
    data = json.load(f)

print(f"原始数据长度: {len(data)}")

# 添加第135章
new_chapter = {
    "number": 135,
    "title": "测试章节",
    "status": "pending",
    "wordCount": 374,
    "createdAt": datetime.now().isoformat() + "Z",
    "updatedAt": datetime.now().isoformat() + "Z",
    "auditIssues": [],
    "lengthWarnings": [],
    "lengthTelemetry": {
        "target": 3000,
        "softMin": 2591,
        "softMax": 3409,
        "hardMin": 2182,
        "hardMax": 3818,
        "countingMode": "zh_chars",
        "writerCount": 374,
        "postWriterNormalizeCount": 374,
        "postReviseCount": 0,
        "finalCount": 374,
        "normalizeApplied": False,
        "lengthWarning": True
    },
    "tokenUsage": {
        "promptTokens": 0,
        "completionTokens": 0,
        "totalTokens": 0
    }
}

# 确保按章节号排序
data.append(new_chapter)
data.sort(key=lambda x: x['number'])

print(f"更新后数据长度: {len(data)}")

# 保存文件
with open(index_path, 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=2)

print("成功添加第135章到 index.json")