"""
模块入口：

  python -m backboneagent

默认读取当前目录的：
- project_report.txt

并输出一个 markdown 文件（默认 backbone_from_report.md）。
"""

from __future__ import annotations

import argparse
import json

from .runner import BackboneAgentConfig, generate_backbone_md


def _parse_args() -> BackboneAgentConfig:
    p = argparse.ArgumentParser(prog="backboneagent", add_help=True)
    p.add_argument("--report", default="project_report.txt", help="Path to project_report.txt")
    p.add_argument("--out", default="backbone_from_report.md", help="Output markdown path")
    p.add_argument("--architecture-json", default="architecture.json", help="Path to architecture.json (from ModuleAtlas)")
    p.add_argument("--architecture-md", default="architecture.md", help="Path to architecture.md (from ModuleAtlas)")
    p.add_argument("--max-input-chars", type=int, default=220000, help="Cap filtered report chars sent to LLM")
    p.add_argument("--format", choices=["json", "quiet"], default="quiet", help="Print JSON meta or stay quiet")
    args = p.parse_args()
    cfg = BackboneAgentConfig(
        report_path=str(args.report),
        out_md_path=str(args.out),
        architecture_json_path=str(args.architecture_json),
        architecture_md_path=str(args.architecture_md),
        max_input_chars=int(args.max_input_chars),
    )
    # stash for main printing
    cfg._print_format = str(args.format)  # type: ignore[attr-defined]
    return cfg


def main() -> int:
    cfg = _parse_args()
    res = generate_backbone_md(cfg)
    fmt = getattr(cfg, "_print_format", "quiet")
    if fmt == "json":
        print(json.dumps(res, ensure_ascii=False, indent=2))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

