#!/usr/bin/env python3
"""raucle-gate: QM deployment tool that routes a tool call through the raucle gate.

Installed by QM's deployment-layer sync per tools/raucle-gate/tool.json
(install.files contract: UTF-8 text file, destination /usr/local/bin/raucle-gate,
mode 0755). Standard library only; no third-party dependencies.

Environment (set by the deployment's scope env or the calling skill):
  RAUCLE_GATEWAY_URL       gate origin, e.g. http://raucle-gateway:8080
  RAUCLE_CAPABILITY_TOKEN  the per-task capability token (the JSON minted by the
                           raucle bridge), forwarded as X-Capability-Token

Usage:
  raucle-gate <tool> <args-json>

Exit codes:
  0  allow     the call was allowed; the response body (with receipt_id) is on stdout
  1  deny      the call was denied; response body with the receipt id is on stdout
  2  escalate  the call requires a human approval; response body on stdout
  3  error     the gate was unreachable or the response was not understood.
               Fail closed: the wrapped action must NOT proceed.
"""

from __future__ import annotations

import json
import os
import sys
import urllib.error
import urllib.request

_TIMEOUT_S = 15
_EXIT = {"allow": 0, "deny": 1, "escalate": 2}


def _retry_guidance(token: dict) -> dict | None:
    """raucle/retry-guidance/v1, derived from the token's own constraints.

    Mirrors raucle.integrations.retry_guidance (a deliberate stdlib copy:
    the installed sandbox artifact cannot import raucle). Deterministic
    from the whitelist/required/bounds constraints the gate enforced;
    ``None`` - the key is simply never added - when nothing positive is
    derivable (denylists, rate limits, expiry windows).
    """
    tool = token.get("tool")
    if not isinstance(tool, str) or not tool:
        return None
    constraints = token.get("constraints")
    if not isinstance(constraints, dict):
        constraints = {}
    criteria: dict = {}
    allowed = constraints.get("allowed_values")
    if isinstance(allowed, dict):
        whitelists = {
            str(f): list(v)
            for f, v in allowed.items()
            if isinstance(v, list) and v and all(isinstance(x, (str, int, bool)) for x in v)
        }
        if whitelists:
            criteria["allowed_values"] = whitelists
    required = constraints.get("required_present")
    if isinstance(required, list) and required:
        criteria["required_present"] = [str(f) for f in required]
    bounds: dict = {}
    for key, side in (("min_value", "min"), ("max_value", "max")):
        for field, bound in (constraints.get(key) or {}).items():
            if isinstance(bound, (int, float)) and not isinstance(bound, bool):
                bounds.setdefault(str(field), {})[side] = bound
    if bounds:
        criteria["bounds"] = bounds
    if not criteria:
        return None
    return {
        "schema": "raucle/retry-guidance/v1",
        "permitted_actions": [{"tool": tool, "criteria": criteria}],
    }


def _attach_guidance(body: dict, token: dict) -> dict:
    """Additive on deny only: verdict fields pass through byte-identical,
    allow/escalate responses are returned unchanged."""
    if body.get("decision") != "deny":
        return body
    guidance = _retry_guidance(token)
    if guidance is None:
        return body
    out = dict(body)
    out["retry_guidance"] = guidance
    return out


def _fail_closed(message: str) -> int:
    print(json.dumps({"decision": "deny", "reason": "gate_unreachable", "detail": message}))
    print(message, file=sys.stderr)
    return 3


def main() -> int:
    if len(sys.argv) != 3:
        print("usage: raucle-gate <tool> <args-json>", file=sys.stderr)
        return 3

    tool, args_json = sys.argv[1], sys.argv[2]
    try:
        args = json.loads(args_json)
        if not isinstance(args, dict):
            raise ValueError("args must be a JSON object")
    except ValueError as exc:
        print(f"invalid args json: {exc}", file=sys.stderr)
        return 3

    url = os.environ.get("RAUCLE_GATEWAY_URL")
    token = os.environ.get("RAUCLE_CAPABILITY_TOKEN")
    if not url or not token:
        print(
            "RAUCLE_GATEWAY_URL and RAUCLE_CAPABILITY_TOKEN must both be set",
            file=sys.stderr,
        )
        return 3

    try:
        agent_id = json.loads(token)["agent_id"]
    except (ValueError, KeyError, TypeError):
        print("RAUCLE_CAPABILITY_TOKEN is not a valid capability token", file=sys.stderr)
        return 3

    payload = json.dumps({"tool": tool, "args": args, "agent_id": agent_id}).encode()
    req = urllib.request.Request(
        url.rstrip("/") + "/gate",
        data=payload,
        headers={
            "Content-Type": "application/json",
            "X-Capability-Token": token,
        },
    )

    try:
        with urllib.request.urlopen(req, timeout=_TIMEOUT_S) as resp:
            body = json.loads(resp.read())
    except urllib.error.HTTPError as exc:
        # The gate answers auth/policy failures with a receipted DENY payload,
        # so an HTTP error status still carries a decision body.
        try:
            body = json.loads(exc.read())
        except (ValueError, OSError):
            return _fail_closed(f"gate http {exc.code} without a decision body")
    except (urllib.error.URLError, TimeoutError, OSError) as exc:
        return _fail_closed(f"gate unreachable: {exc}")

    print(json.dumps(_attach_guidance(body, json.loads(token))))
    return _EXIT.get(body.get("decision", ""), 3)


if __name__ == "__main__":
    sys.exit(main())