"""Append a structured failure note for a trial or episode."""

from __future__ import annotations

import argparse
import json
from datetime import datetime, timezone
from pathlib import Path


def main() -> None:
    parser = argparse.ArgumentParser(description="Record a failure diagnosis.")
    parser.add_argument("--failure-type", required=True)
    parser.add_argument("--evidence", required=True)
    parser.add_argument("--hypothesis", required=True)
    parser.add_argument("--next-step", required=True)
    parser.add_argument("--trial-index", type=int)
    parser.add_argument("--episode")
    parser.add_argument("--video-path")
    parser.add_argument("--path", type=Path, default=Path("failures.jsonl"))
    args = parser.parse_args()

    record = {
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "trial_index": args.trial_index,
        "episode": args.episode,
        "failure_type": args.failure_type,
        "evidence": args.evidence,
        "hypothesis": args.hypothesis,
        "next_step": args.next_step,
        "video_path": args.video_path,
    }
    with args.path.open("a", encoding="utf-8") as handle:
        handle.write(json.dumps(record, separators=(",", ":")))
        handle.write("\n")
    print(f"failure appended: {args.path}")


if __name__ == "__main__":
    main()
