"""替换文档中商品域和库存域的内容"""

target = "docs/reports/dwd-table-structure-overview.md"
source = "_tmp_goods_inventory_section.md"

with open(target, "r", encoding="utf-8") as f:
    lines = f.readlines()

with open(source, "r", encoding="utf-8") as f:
    new_content = f.read()

# 找到 ## 7 和 ## 9 的行号
start_idx = None
end_idx = None
for i, line in enumerate(lines):
    if line.startswith("## 7. 商品域"):
        start_idx = i
    elif line.startswith("## 9. 门店与员工维度"):
        end_idx = i
        break

if start_idx is None or end_idx is None:
    print(f"ERROR: start={start_idx}, end={end_idx}")
else:
    # 确保新内容末尾有换行
    if not new_content.endswith("\n\n"):
        new_content = new_content.rstrip("\n") + "\n\n"
    result = lines[:start_idx] + [new_content] + lines[end_idx:]
    with open(target, "w", encoding="utf-8") as f:
        f.writelines(result)
    print(f"OK: 替换了行 {start_idx+1}~{end_idx} -> 新内容 {len(new_content)} 字符")
