#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "pydantic==2.11.7",
# ]
# ///

"""
Takes the raw json file from all packages we tested, and removes all packages where no cache files
were found.
"""

import json
from pathlib import Path

from models import Package, Distribution, CacheFile, DistributionKind

INPUT_PATH = Path(__file__).parent.parent.parent.joinpath("2025-09-09-results.json")


def main():
    with INPUT_PATH.open() as fp:
        data = json.load(fp)
    packages = []
    for package_name, package_info in data.items():
        distributions = []
        for dist_kind in list(DistributionKind):
            if dist_kind in package_info and package_info[dist_kind]["files"]:
                distributions.append(
                    Distribution(
                        kind=dist_kind,
                        dist_path=Path(package_info[dist_kind]["filepath"]),
                        cache_files=[
                            CacheFile.model_validate(file)
                            for file in package_info[dist_kind]["files"]
                        ],
                    )
                )
        if distributions:
            packages.append(
                Package(
                    name=package_name,
                    version=package_info["meta"]["version"],
                    proton_path=package_info["meta"]["base_path"],
                    distributions=distributions,
                )
            )
    results_dir = Path(__file__).parent.joinpath("results")
    results_dir.mkdir(exist_ok=True)
    with results_dir.joinpath("compact.json").open("w") as fp:
        json.dump([p.model_dump() for p in packages], fp, indent=2)


if __name__ == "__main__":
    main()
