"""Generate the eight level files the game loads.

    python data/make_levels.py            # writes web/levels/<id>.json and web/levels/index.json

The datasets are synthetic, made with fixed seeds in hunt/levels.py, so running this again
gives byte-identical files. Sizes are kept small: image masks are packed bits in base64.
"""
from __future__ import annotations

import json
import os
import sys

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)

from hunt import levels  # noqa: E402


def main():
    out_dir = os.path.join(ROOT, "web", "levels")
    os.makedirs(out_dir, exist_ok=True)
    index = []
    for lv in levels.make_all():
        path = os.path.join(out_dir, lv["id"] + ".json")
        with open(path, "w", encoding="utf-8") as f:
            json.dump(lv, f, separators=(",", ":"))
        size = os.path.getsize(path)
        index.append({"id": lv["id"], "n": lv["n"], "file": lv["id"] + ".json", "bytes": size})
        print("%-12s %7.1f KB" % (lv["id"], size / 1024))
    with open(os.path.join(out_dir, "index.json"), "w", encoding="utf-8") as f:
        json.dump(index, f, indent=1)


if __name__ == "__main__":
    main()
