"""
Phase 1: 招标文件解析 — 从历史投标文件提取目录结构

用法:
    from phase1_parsing.extract_bid_toc import extract_toc
    toc = extract_toc("投标文件_extracted.txt")
"""

import os


def extract_toc(text: str) -> list:
    """
    从历史投标文件的文本中提取目录结构。

    目录行通常包含 "..." + 页码，按缩进判断层级。

    Args:
        text: 历史投标文件的文本内容

    Returns:
        [{"title": "章节名", "level": 1-3, "page": "页码"}, ...]
    """
    lines = text.split("\n")
    toc_lines = []
    toc_started = False
    toc_start_keywords = ["目  录", "目录", "商务部分"]

    for i, line in enumerate(lines[:300]):
        line_stripped = line.strip()
        if not line_stripped:
            continue

        if any(kw in line_stripped for kw in toc_start_keywords):
            toc_started = True
            continue
        if not toc_started:
            continue
        if line_stripped.startswith("第一章") and len(toc_lines) > 3:
            break

        if "..." in line_stripped and any(c.isdigit() for c in line_stripped):
            title = line_stripped.split("...")[0].strip()
            indent = len(line) - len(line.lstrip(" "))
            level = 3 if indent > 5 else (2 if indent > 2 else 1)
            page_num = ""
            parts = line_stripped.split("...")
            if parts:
                last_part = parts[-1].strip()
                if last_part.isdigit():
                    page_num = last_part
            toc_lines.append({
                "title": title,
                "level": level,
                "page": page_num,
            })

    return toc_lines


if __name__ == "__main__":
    import sys
    sys.stdout.reconfigure(encoding="utf-8")

    with open("松江区机关事务管理局物业管理服务项目投标文件_extracted.txt",
              "r", encoding="utf-8") as f:
        text = f.read()

    toc = extract_toc(text)
    print(f"提取到 {len(toc)} 行目录")
    for item in toc[:30]:
        indent = "  " * (item["level"] - 1)
        print(f"  {indent}{item['title']} (p.{item['page']})")
