#!/usr/bin/env bash
# Validate datapack metadata: pack.mcmeta is JSON with pack.pack_format,
# and every .json file under data/ parses as JSON.
set -euo pipefail
root="$(cd "$(dirname "$0")/../.." && pwd)"
cd "$root"
python3 - <<'PY'
import json, sys
from pathlib import Path

root = Path(".")
errors = []

meta_path = root / "pack.mcmeta"
if not meta_path.is_file():
    errors.append("pack.mcmeta ontbreekt")
else:
    try:
        meta = json.loads(meta_path.read_text(encoding="utf-8"))
    except json.JSONDecodeError as e:
        errors.append(f"pack.mcmeta is geen geldige JSON: {e}")
        meta = None
    if meta is not None:
        pack = meta.get("pack") if isinstance(meta, dict) else None
        if not isinstance(pack, dict) or "pack_format" not in pack:
            errors.append("pack.mcmeta mist pack.pack_format")
        elif pack["pack_format"] in (None, "", []):
            errors.append("pack.mcmeta heeft een lege pack.pack_format")

data = root / "data"
if not data.is_dir():
    errors.append("data/ ontbreekt")
else:
    files = sorted(data.rglob("*.json"))
    if not files:
        errors.append("geen .json-bestanden onder data/")
    for path in files:
        try:
            json.loads(path.read_text(encoding="utf-8"))
        except json.JSONDecodeError as e:
            errors.append(f"{path}: geen geldige JSON: {e}")

if errors:
    print("Validatie mislukt:", file=sys.stderr)
    for e in errors:
        print(f"  - {e}", file=sys.stderr)
    sys.exit(1)

print("Validatie geslaagd: pack.mcmeta en alle JSON onder data/ zijn in orde.")
PY
