"""Minimal loader for a SignIQ research-preview sample bundle.

Usage:

    python3 load_lerobot.py samples/<sample-id>

Reads manifest.json + metadata.json + health.json and prints a one-page
summary. The dense numeric streams are kept as JSON so this stays
zero-dependency; convert to numpy / parquet downstream as needed.
"""

from __future__ import annotations

import json
import sys
from pathlib import Path


def load_sample(path: Path) -> dict:
    """Read a SignIQ sample bundle directory into a dict."""
    out: dict = {}
    for name in ("manifest", "metadata", "timeseries", "trajectory", "health"):
        f = path / f"{name}.json"
        if f.exists():
            out[name] = json.loads(f.read_text())
    out["videos"] = sorted(p.name for p in (path / "videos").glob("*.mp4"))
    return out


def summarize(sample: dict) -> None:
    m = sample["manifest"]
    md = sample.get("metadata", {})
    h = sample.get("health", {})
    schema = (md.get("schema") or {})
    state = schema.get("observation.state", {})
    action = schema.get("action", {})
    pass_count = sum(1 for c in h.get("checks", []) if c.get("status") == "pass")
    print(f"Sample      : {m['name']}")
    print(f"Robot       : {m['robot']}")
    print(f"Task family : {m['task_family']}")
    print(f"Control     : {m['control_hz']} Hz · {m['frame_count']} frames · {m['duration_seconds']}s")
    print(f"Cameras     : {len(m['cameras'])}  ({', '.join(c['id'] for c in m['cameras'])})")
    if state:
        print(f"State dim   : {state.get('shape')}")
    if action:
        print(f"Action dim  : {action.get('shape')}  ({action.get('kind', 'joint_target')})")
    print(f"Modalities  : {' · '.join(m.get('modalities', []))}")
    print(f"Format      : {' · '.join(m.get('format_exports', []))}")
    print(f"License     : {m['license']}")
    print(f"QA          : {pass_count}/{len(h.get('checks', []))} checks pass")


def main() -> None:
    if len(sys.argv) < 2:
        print("Usage: python3 load_lerobot.py <sample-dir>")
        sys.exit(1)
    path = Path(sys.argv[1])
    if not path.exists():
        print(f"Path does not exist: {path}")
        sys.exit(1)
    summarize(load_sample(path))


if __name__ == "__main__":
    main()
