from __future__ import annotations

import argparse
import json
from pathlib import Path

from klt51_core import analyze_file, generate_blueprint_svg, generate_chart_svg, generate_report, generate_scheme_svg


def main() -> None:
    parser = argparse.ArgumentParser(description="KLT 5.1 reference CLI")
    parser.add_argument("project_json")
    parser.add_argument("--out", default="out")
    args = parser.parse_args()

    out = Path(args.out)
    out.mkdir(parents=True, exist_ok=True)

    result = analyze_file(args.project_json)
    (out / "result.json").write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
    (out / "report.md").write_text(generate_report(result), encoding="utf-8")
    (out / "scheme.svg").write_text(generate_scheme_svg(result), encoding="utf-8")
    (out / "blueprint.svg").write_text(generate_blueprint_svg(result), encoding="utf-8")
    (out / "graph.svg").write_text(generate_chart_svg(result), encoding="utf-8")
    print(json.dumps({
        "pix_count": result["pix_count"],
        "edge_count": result["edge_count"],
        "defect_count": result["defect_count"],
        "lambda_count": result["lambda_count"],
        "cri": round(result["cri"], 4),
        "out": str(out),
    }, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
