#!/usr/bin/env python3
"""Report or persist the current codebase modularization/performance inventory."""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parents[2]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from services.codebase_modularization_performance_service import (  # noqa: E402
    build_codebase_inventory,
    write_codebase_inventory_receipt,
)


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--write", action="store_true", help="persist the latest runtime receipt")
    args = parser.parse_args()
    result = build_codebase_inventory()
    if args.write:
        result["receipt_path"] = str(write_codebase_inventory_receipt(result))
    print(json.dumps(result, ensure_ascii=False, indent=2))
    return 0


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