#!/usr/bin/env python3
"""Read-only DARKOZA partner preflight. Uses only the Python standard library."""
from __future__ import annotations
import argparse, json, sys
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
DEFAULT_BASE = "https://darkoza.com"
def get_json(base: str, path: str, timeout: float) -> dict:
    request = Request(base.rstrip("/") + path, headers={"Accept":"application/json"}, method="GET")
    with urlopen(request, timeout=timeout) as response:
        if response.status != 200:
            raise RuntimeError(f"{path}: HTTP {response.status}")
        return json.loads(response.read().decode("utf-8"))
def main() -> int:
    parser = argparse.ArgumentParser(description="Read-only DARKOZA Robotics API preflight")
    parser.add_argument("--base-url", default=DEFAULT_BASE)
    parser.add_argument("--timeout", type=float, default=8.0)
    args = parser.parse_args()
    try:
        health = get_json(args.base_url, "/robotics-api/health", args.timeout)
        catalog = get_json(args.base_url, "/robotics-api/v1/capabilities", args.timeout)
        checks = {
            "api_healthy": health.get("status") == "ok",
            "api_planning_only": health.get("execution_mode") == "planning_only" and catalog.get("execution_mode") == "planning_only",
            "no_physical_robot_claimed": health.get("hardware_connected") is False,
            "physical_actuation_disabled": health.get("physical_actuation") is False and catalog.get("robot_profile_contract", {}).get("actuation_enabled") is False,
            "task_dispatch_disabled": health.get("task_dispatch") is False and catalog.get("mission_planning", {}).get("dispatch") is False,
            "profile_contract_published": catalog.get("robot_profile_contract", {}).get("schema") == "darkoza.robot-profile/v1",
            "catalog_complete": all(isinstance(catalog.get(k), list) for k in ("capabilities","vendors","embodiments")),
        }
        print("DARKOZA Robotics partner preflight")
        print("API version:", health.get("api_version", "unknown"))
        print("Capabilities:", len(catalog.get("capabilities", [])))
        print("Vendors:", len(catalog.get("vendors", [])))
        print("Embodiments:", len(catalog.get("embodiments", [])))
        print("Execution mode:", health.get("execution_mode", "unknown"))
        for name, passed in checks.items():
            print("[{}] {}".format("PASS" if passed else "FAIL", name))
        if not all(checks.values()): return 2
        print("Result: API is reachable and the published safety boundary is intact.")
        print("This test is GET-only. It creates no account, robot, task, simulation or hardware connection.")
        print("Next: review the disconnected profile and interface-map examples.")
        return 0
    except (HTTPError, URLError, TimeoutError, ValueError, RuntimeError) as exc:
        print("Preflight failed:", exc, file=sys.stderr)
        return 1
if __name__ == "__main__":
    raise SystemExit(main())
