#!/usr/bin/env python3
"""Summarize the state and products of the reproducible ILAMB study."""

from __future__ import annotations

import argparse
import json
from pathlib import Path


DEFAULT_WORKDIR = Path(
    "/projects/hpcl-cli185/proj-shared/zdr/ilamb_runs/hydrology_snow_1980_2022"
)


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--workdir", type=Path, default=DEFAULT_WORKDIR)
    args = parser.parse_args()
    manifest_path = args.workdir / "manifest.json"
    if manifest_path.is_file():
        manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
        print(
            "study_years_inclusive:",
            "-".join(str(year) for year in manifest["study_years_inclusive"]),
        )
        for model in manifest["models"]:
            print(
                f"model: {model['path']} files={model['file_count']} "
                f"grid={model['grid']['lat']}x{model['grid']['lon']}"
            )
    job_path = args.workdir / "job_id.txt"
    print("job_id:", job_path.read_text().strip() if job_path.is_file() else "not submitted")
    build = args.workdir / "build"
    if not build.is_dir():
        print("build: not created")
        return
    patterns = ("*.html", "*.nc", "*.png", "*.log", "*.pkl")
    for pattern in patterns:
        files = list(build.rglob(pattern))
        print(f"{pattern}: {len(files)}")
    index = build / "index.html"
    print("index:", index if index.is_file() else "not yet generated")


if __name__ == "__main__":
    main()
