#!/usr/bin/env python3
from __future__ import annotations

import argparse
import hashlib
import json
from pathlib import Path


def digest(path: Path) -> str:
    return hashlib.sha256(path.read_bytes()).hexdigest()


parser = argparse.ArgumentParser()
parser.add_argument("--output", type=Path, required=True)
arguments = parser.parse_args()
root = arguments.output
root.mkdir(parents=True)
(root / "nested").mkdir()
payload = bytearray()
for offset in range(384 * 1024):
    payload.append(((offset * 131) ^ (offset >> 3) ^ 0x5A) & 0xFF)
(root / "payload.bin").write_bytes(payload)
(root / "nested" / "alpha.txt").write_text(
    "Pre15 deterministic smoke fixture\n", encoding="ascii"
)
(root / "empty").write_bytes(b"")
files = []
for path in sorted(item for item in root.rglob("*") if item.is_file()):
    files.append(
        {
            "path": path.relative_to(root).as_posix(),
            "sha256": digest(path),
            "size": path.stat().st_size,
        }
    )
manifest = {
    "files": files,
    "nested_entries": ["alpha.txt"],
    "root_entries": ["empty", "nested", "payload.bin"],
    "status": "READY",
}
print(json.dumps(manifest, indent=2, sort_keys=True))
