"""CLI entry point for primust-discover."""

from __future__ import annotations

import json
import sys


def main() -> None:
    import argparse

    parser = argparse.ArgumentParser(
        prog="primust-discover",
        description="Discover governance decision points in a Python codebase via static analysis.",
    )
    parser.add_argument("path", help="Path to codebase root")
    parser.add_argument(
        "--output",
        default=None,
        help="Directory for generated manifest stubs (default: no manifest generation)",
    )
    parser.add_argument(
        "--json",
        action="store_true",
        dest="json_output",
        help="Output as JSON instead of formatted report",
    )
    args = parser.parse_args()

    from primust_discover.analyzer import discover, format_report

    try:
        report = discover(root_path=args.path, output_dir=args.output)
    except FileNotFoundError as e:
        print(f"Error: {e}", file=sys.stderr)
        sys.exit(1)

    if args.json_output:
        data = {
            "root_path": report.root_path,
            "files_analyzed": report.files_analyzed,
            "lines_analyzed": report.lines_analyzed,
            "decision_points_count": report.count,
            "decision_points": [
                {
                    "file": dp.file,
                    "line": dp.line,
                    "function_name": dp.function_name,
                    "pattern": dp.pattern.name,
                    "proof_level": dp.pattern.proof_level,
                    "category": dp.pattern.category,
                    "suggested_manifest_id": dp.suggested_manifest_id,
                }
                for dp in report.decision_points
            ],
            "manifests_generated": report.manifests_generated,
        }
        print(json.dumps(data, indent=2))
    else:
        print(format_report(report))


if __name__ == "__main__":
    main()
