"""Einstiegspunkt: python -m scripts <image_dir> [--robot ...] [--cameras ...]"""
import argparse
import json
import sys


def main() -> None:
    ap = argparse.ArgumentParser(
        description="appRobotBodyTrack: Bilder + robot.json → Gelenkwinkel im R⁷"
    )
    ap.add_argument("image_dir", help="Ordner mit render_*.png/jpg und render_*.npz")
    ap.add_argument("--robot", default=None, help="Pfad zu robot.json")
    ap.add_argument("--evalDir", default=None, help="Ausgabeordner (Standard: temporäres Verzeichnis)")
    ap.add_argument("--cameras", default=None, help="Kamera-IDs, kommagetrennt, z.B. a,b,d")
    ap.add_argument("--lambdaWeight", type=float, default=100.0)
    args = ap.parse_args()

    from scripts import estimate_from_dir

    camera_filter = args.cameras.split(",") if args.cameras else None
    try:
        result = estimate_from_dir(
            args.image_dir,
            robot_json=args.robot,
            eval_dir=args.evalDir,
            lambda_weight=args.lambdaWeight,
            camera_filter=camera_filter,
        )
    except Exception as exc:
        print(f"[ERROR] {exc}", file=sys.stderr)
        sys.exit(1)

    print(json.dumps({
        "joints": result.joints,
        "confidence": result.confidence,
        "n_markers": result.n_markers,
        "residual_rms": result.residual_rms,
        "processing_ms": result.processing_ms,
    }, indent=2, ensure_ascii=False))


if __name__ == "__main__":
    main()
