#!/usr/bin/env python3
import json
from pathlib import Path


def policy(action, approved):
    if action["kind"] == "write" and not approved:
        return {"allowed": False, "reason": "approval_required"}
    return {"allowed": True}


candidate = {"kind": "write", "resource": "order_demo", "value": "refunded"}
model_driven = {"candidate": candidate, "policy": policy(candidate, approved=False), "terminal": "blocked"}

graph_trace = [
    {"node": "read_order", "status": "completed"},
    {"node": "request_approval", "status": "approved"},
]
decision = policy(candidate, approved=True)
graph_trace.append({"node": "write_refund", "status": "completed" if decision["allowed"] else "blocked"})
graph_driven = {"trace": graph_trace, "terminal": "success" if decision["allowed"] else "blocked"}

result = {"model_driven_without_gate": model_driven, "graph_driven_with_gate": graph_driven}
Path(__file__).with_name("experiment-result.json").write_text(
    json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
)
print(json.dumps(result, ensure_ascii=False, indent=2))
