#!/usr/bin/env python3
"""Rebuild a QoR manifest summary from existing raw Vivado reports."""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

from vivado_qor_collect import summarize_reports


def main(argv: list[str]) -> int:
    parser = argparse.ArgumentParser(description="Resummarize existing QoR output")
    parser.add_argument("out_dir", help="QoR output directory containing raw reports")
    args = parser.parse_args(argv)

    out_dir = Path(args.out_dir).resolve()
    manifest_path = out_dir / "manifest.json"
    manifest = json.loads(manifest_path.read_text(encoding="utf-8")) if manifest_path.exists() else {}
    manifest["summary"] = summarize_reports(out_dir)
    manifest_path.write_text(json.dumps(manifest, indent=2), encoding="utf-8")
    print(json.dumps(manifest["summary"], indent=2))
    return 0


if __name__ == "__main__":
    raise SystemExit(main(sys.argv[1:]))
