#!/usr/bin/env python3
"""Stub ``sensibo`` CLI for demo-3 (self-hosted mode only).

Mirrors the real `sensibo` CLI's argv shape (`sensibo set <pod> --flags...
--apply`, `sensibo read <pod>`) closely enough for the board's dashboard
pipeline (reterminal/board/dashboard.py) to drive it end-to-end with no
network calls and no physical AC involved.

Every invocation is appended as one JSON line of argv to
``$SENSIBO_STUB_CALLS`` (default: ``<this dir>/calls.jsonl``) so a demo script
or test can assert on exactly what the board ran.

This script MUST be the only thing named ``sensibo`` reachable on PATH when a
demo runs in self-hosted mode -- callers arrange that by prepending this
directory to PATH. It never talks to the real Sensibo cloud.
"""

from __future__ import annotations

import json
import os
import sys
from pathlib import Path

CALLS_PATH = Path(os.environ.get("SENSIBO_STUB_CALLS") or (Path(__file__).parent / "calls.jsonl"))


def main() -> int:
    argv = sys.argv[1:]
    CALLS_PATH.parent.mkdir(parents=True, exist_ok=True)
    with CALLS_PATH.open("a", encoding="utf-8") as fh:
        fh.write(json.dumps(argv) + "\n")

    if argv[:1] == ["read"]:
        print("24.5C")
    else:
        print(json.dumps({"ok": True, "argv": argv}))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
