from __future__ import annotations

import argparse
import sys

from .graph import build_graph


def main() -> None:
    parser = argparse.ArgumentParser(description="AI Landscape Review — LangGraph pipeline")
    parser.add_argument(
        "--period",
        choices=["weekly", "monthly", "quarterly", "yearly"],
        required=True,
    )
    parser.add_argument(
        "--dry-run",
        action="store_true",
        help="Print synthesis without writing to file",
    )
    args = parser.parse_args()

    print(f"ai-landscape-review: starting {args.period} review", file=sys.stderr)

    graph = build_graph()
    initial_state = {
        "period": args.period,
        "dry_run": args.dry_run,
        "raw_articles": "",
        "previous_entries": "",
        "gaps": "",
        "research": "",
        "synthesis": "",
        "written": False,
    }

    try:
        result = graph.invoke(initial_state)
    except Exception as exc:
        print(f"ai-landscape-review: failed: {exc}", file=sys.stderr)
        raise SystemExit(1) from exc

    if args.dry_run:
        print(result["synthesis"])
    else:
        print(
            f"ai-landscape-review: {args.period} snapshot written to AI Landscape.md",
            file=sys.stderr,
        )


if __name__ == "__main__":
    main()
