"""Poll /healthz until the app reports the known marker, or time out."""

import sys
import time

from _client import call

MARKER = "OAI-TOYAPP-HEALTHY"


def wait(timeout_seconds=60):
    deadline = time.time() + timeout_seconds
    last_error = None
    while time.time() < deadline:
        try:
            status, body = call("GET", "/healthz", timeout=3)
            if status == 200 and body.get("marker") == MARKER:
                return body
        except Exception as exc:  # noqa: BLE001 - report the last failure
            last_error = exc
        time.sleep(1.5)
    raise SystemExit(f"health check timed out after {timeout_seconds}s: {last_error}")


if __name__ == "__main__":
    body = wait()
    print(f"[wait_for_health] healthy: {body}")
    sys.exit(0)
