#!/usr/bin/env python3
"""Cabinets demo proxy: one rule in words changes three running applications at once, with the trace of each.

127.0.0.1:18452 behind Caddy (handle /api/cabinets/*). The platform token stays in the environment.

  GET  /api/cabinets/state                 deals, cabinets (programs + sha), default rules, devices, model
  POST /api/cabinets/evaluate              {session, device: vps|mac, force?} -> three cabinets evaluated on the device with the session's rules
  POST /api/cabinets/rule                  {wish, scope: [...], session} -> the model writes a policy rule, grammar check, rule added to the session, cabinets re-evaluated
  POST /api/cabinets/rule-json             {rule, session} -> same path without the model
  POST /api/cabinets/rule/remove           {id, session}
  POST /api/cabinets/reset                 {session} -> back to the default rules
  GET  /api/cabinets/export/<cabinet>      bundle: handler + cabinet program
"""
import hashlib
import json
import os
import sys
import threading
import time
import urllib.error
import urllib.request
from concurrent.futures import ThreadPoolExecutor
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import cabinet_core as core  # noqa: E402

API = "https://api.extella.ai/api"
TOKEN = os.environ["EXTELLA_TOKEN"]
AGENT = os.environ.get("ROBOT_AGENT", "agent_Fgw-hPvLsg9vyn2QDg5GO")
DEVICES = {"vps": {"id": os.environ.get("ROBOT_DEVICE", "85800354-f7b7-449f-b526-9357cd91f780"), "name": "extella-team", "label": "Server 1 · Linux"},
           "second": {"id": os.environ.get("ROBOT_DEVICE_SECOND", "9ed87a11-61f9-4985-b997-584de73d028f"), "name": "extella-second", "label": "Server 2 · Linux"},
           "mac": {"id": os.environ.get("ROBOT_DEVICE_MAC", "24f37e45-8c9f-4896-b64f-0dcd0cd8b0e4"), "name": "MacBook", "label": "MacBook · macOS"}}
OPENROUTER_KEY = os.environ.get("OPENROUTER_API_KEY", "")
MODEL = os.environ.get("ROBOT_MODEL", "google/gemini-2.5-flash-lite")
PORT = int(os.environ.get("CABINETS_PORT", "18452"))
DAY_LIMIT = int(os.environ.get("ROBOT_DAY_LIMIT", "3000"))
SESSION_LIMIT = int(os.environ.get("ROBOT_SESSION_LIMIT", "10"))
HERE = os.path.dirname(os.path.abspath(__file__))
CABINETS = ["lawyer", "finance", "logistics"]

LOCK = threading.Lock()
STATE = {"day": time.strftime("%Y-%m-%d"), "generations_today": 0, "sessions": {}, "rate": {}, "cache": {}}
CALLS = threading.local()
POOL = ThreadPoolExecutor(max_workers=6)


def calls_reset():
    CALLS.n = 0


def calls_count():
    return getattr(CALLS, "n", 0)


def platform(path, body, timeout=120):
    req = urllib.request.Request(API + path, data=json.dumps(body, ensure_ascii=False).encode(),
                                 headers={"X-Auth-Token": TOKEN, "X-Profile-Id": "default", "X-Agent-Id": AGENT, "Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=timeout) as r:
        return json.loads(r.read())


def unwrap(out):
    import ast
    res = out.get("result", out) if isinstance(out, dict) else out
    if isinstance(res, dict) and "result" in res:
        res = res["result"]
    if isinstance(res, str):
        for loader in (json.loads, ast.literal_eval):
            try:
                return loader(res)
            except Exception:
                pass
    return res


def run_expert(expert, params, device):
    dev = DEVICES.get(device, DEVICES["vps"])
    t0 = time.time()
    try:
        out = platform("/expert/run", {"name": expert, "params": params, "targets": [dev["id"]]}, timeout=150)
    except urllib.error.HTTPError as e:
        body = e.read().decode("utf-8", "ignore")[:200]
        return {"status": "error", "reason": ("device %s is offline right now" % dev["name"]) if "unavailable" in body.lower() else "platform error: " + body, "platform_seconds": round(time.time() - t0, 1)}
    except Exception as e:
        return {"status": "error", "reason": "platform did not answer: " + str(e)[:120], "platform_seconds": round(time.time() - t0, 1)}
    task = out.get("task_id") if isinstance(out, dict) else None
    while task:
        time.sleep(2)
        chk = platform("/tasks/check", {"task_id": task}, timeout=30)
        if str(chk.get("status")) in ("completed", "done", "success", "finished", "error", "failed"):
            out = chk
            break
    res = unwrap(out)
    if not isinstance(res, dict):
        res = {"status": "error", "reason": str(res)[:200]}
    res["platform_seconds"] = round(time.time() - t0, 1)
    if isinstance(res.get("reason"), str) and "unavailable" in res["reason"].lower():
        res["status"] = "error"
        res["reason"] = "device %s is offline right now" % dev["name"]
    return res


def registrar_sha():
    try:
        reg = open(os.path.join(HERE, "demo_register_cabinet.fython"), encoding="utf-8").read()
        body = reg.split("'" * 3)[1]
        lines = [ln for ln in body.split(chr(10)) if not ln.startswith("HANDLER_SHA = ")]
        return hashlib.sha256(chr(10).join(lines).encode("utf-8")).hexdigest()
    except Exception:
        return None


# ---------------------------------------------------------------- sessions: each visitor has their own rule set

def allowed(ip):
    now = time.time()
    with LOCK:
        hits = [t for t in STATE["rate"].get(ip, []) if now - t < 60]
        hits.append(now)
        STATE["rate"][ip] = hits
        return len(hits) <= 60


def session(sid):
    with LOCK:
        if STATE["day"] != time.strftime("%Y-%m-%d"):
            STATE["day"] = time.strftime("%Y-%m-%d")
            STATE["generations_today"] = 0
        return STATE["sessions"].setdefault(sid, {"generations": 0, "rules": [json.loads(json.dumps(r)) for r in core.DEFAULT_RULES], "history": []})


def take_generation(sid):
    s = session(sid)
    with LOCK:
        if STATE["generations_today"] >= DAY_LIMIT:
            return "today's generation budget is spent; try tomorrow"
        if s["generations"] >= SESSION_LIMIT:
            return "this session used its %d generations" % SESSION_LIMIT
        s["generations"] += 1
        STATE["generations_today"] += 1
    return None


def evaluate_all(rules, device, force=False):
    """Run the three cabinet experts on the device in parallel with the given rules. Cached by (cabinet, device, rules sha)."""
    rsha = core.sha(rules)
    results = {}
    todo = []
    for key in CABINETS:
        ck = (key, device, rsha)
        with LOCK:
            hit = None if force else STATE["cache"].get(ck)
        if hit:
            r = dict(hit)
            r["cached"] = True
            results[key] = r
        else:
            todo.append((key, ck))
    if todo:
        futs = {POOL.submit(run_expert, "cabinet_" + key, {"rules": rules}, device): (key, ck) for key, ck in todo}
        for fut, (key, ck) in futs.items():
            r = fut.result()
            r["cached"] = False
            if r.get("status") == "success":
                with LOCK:
                    STATE["cache"][ck] = r
            results[key] = r
    return results


SYSTEM_PROMPT = """You turn a company rule written in words into ONE policy rule in the policy language. Output ONLY a JSON object.
{"id": "<short id, letters and digits>", "scope": [<cabinets the rule applies to, from: lawyer, finance, logistics>], "when": {"field": <field>, "op": <op>, "value": <value>}, "then": "forbid" | "approve_by_head" | "warn", "say": "<the rule restated in one short sentence, same language as the wish>"}
Fields of a deal: client (text), amount (number, money), discount_pct (number, percent), payment_days (number, days of payment delay), delivery_days (number), prepayment_pct (number, percent), country (text, ISO code like DE, PL, TR), has_penalty_clause (true/false).
Ops: > >= < <= == != in not_in (in/not_in take a list).
Choose "then": "forbid" when the wish says forbidden/not allowed/never; "approve_by_head" when it says needs approval/sign-off/head must approve; "warn" when it says flag/warn/pay attention.
If the wish names departments, put only them in scope; otherwise use the scope given by the user message. Use ONLY these fields and ops.
Examples:
"discounts over 10% are forbidden" -> {"id": "disc10", "scope": ["lawyer", "finance", "logistics"], "when": {"field": "discount_pct", "op": ">", "value": 10}, "then": "forbid", "say": "discounts over 10% are forbidden"}
"deals to Turkey need the head's approval, finance and logistics" -> {"id": "tr", "scope": ["finance", "logistics"], "when": {"field": "country", "op": "in", "value": ["TR"]}, "then": "approve_by_head", "say": "deals to Turkey need the head's approval"}
"""


def generate_rule(wish, scope, previous=None, failure=None):
    user = "Wish: " + wish + "\nScope chosen by the user: " + ", ".join(scope)
    if previous is not None:
        user += "\nYour previous rule was rejected: " + str(failure)[:300] + "\nPrevious rule: " + json.dumps(previous, ensure_ascii=False) + "\nFix it using only the grammar."
    body = {"model": MODEL, "temperature": 0.2, "max_tokens": 300, "response_format": {"type": "json_object"},
            "messages": [{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": user}]}
    req = urllib.request.Request("https://openrouter.ai/api/v1/chat/completions", data=json.dumps(body, ensure_ascii=False).encode(),
                                 headers={"Authorization": "Bearer " + OPENROUTER_KEY, "Content-Type": "application/json", "HTTP-Referer": "https://extella.dev/cabinets/", "X-Title": "Extella cabinets demo"})
    t0 = time.time()
    CALLS.n = calls_count() + 1
    with urllib.request.urlopen(req, timeout=60) as r:
        out = json.loads(r.read())
    text = out["choices"][0]["message"]["content"].strip()
    if text.startswith("```"):
        text = text.strip("`")
        text = text[text.find("{"):]
    usage = out.get("usage", {})
    return json.loads(text), {"seconds": round(time.time() - t0, 1), "tokens": usage.get("total_tokens"), "model": out.get("model", MODEL)}


def add_rule_path(rule, sid, wish="", steps=None, device="vps"):
    steps = steps if steps is not None else []
    s = session(sid)
    if isinstance(rule, dict) and isinstance(rule.get("id"), str):
        base = rule["id"][:20]
        ids = {r["id"] for r in s["rules"]}
        n = 2
        while rule["id"] in ids:
            rule["id"] = "%s%d" % (base, n)
            n += 1
    ok, report = core.check_rule(rule)
    steps.append({"step": "grammar", "ok": ok, "seconds": 0.0, "detail": "every word is in the policy language" if ok else report.get("grammar")})
    if not ok:
        return {"status": "failed", "steps": steps, "rule": rule}
    steps.append({"step": "dry_run", "ok": True, "seconds": 0.0, "detail": "deals whose verdict changes: " + json.dumps(report["affects"])})
    with LOCK:
        if len(s["rules"]) >= core.MAX_RULES:
            return {"status": "failed", "steps": steps + [{"step": "add", "ok": False, "detail": "this session already has %d rules; remove one" % core.MAX_RULES}], "rule": rule}
        before = [json.loads(json.dumps(r)) for r in s["rules"]]
        s["rules"].append(rule)
        s["history"].append({"at": time.strftime("%H:%M:%S"), "wish": wish[:200], "rule": rule})
        rules = [json.loads(json.dumps(r)) for r in s["rules"]]
    steps.append({"step": "memory", "ok": True, "seconds": 0.0, "detail": "rule added to your session's rule set (%d rules), sha %s" % (len(rules), core.sha(rules)[:12])})
    t0 = time.time()
    results = evaluate_all(rules, device, force=True)
    ok = all(r.get("status") == "success" for r in results.values())
    steps.append({"step": "evaluate", "ok": ok, "seconds": round(time.time() - t0, 1), "detail": ", ".join("%s on %s" % (k, r.get("host", "?")) for k, r in results.items()) if ok else next((r.get("reason") for r in results.values() if r.get("status") != "success"), "?")})
    return {"status": "success" if ok else "failed", "rule": rule, "rules": rules, "before_rules": before, "affects": report["affects"], "results": results, "steps": steps}


class Handler(BaseHTTPRequestHandler):
    server_version = "extella-cabinets/1"

    def _send(self, code, obj):
        data = json.dumps(obj, ensure_ascii=False).encode()
        self.send_response(code)
        self.send_header("Content-Type", "application/json; charset=utf-8")
        self.send_header("Content-Length", str(len(data)))
        self.send_header("Cache-Control", "no-store")
        self.end_headers()
        self.wfile.write(data)

    def _body(self):
        n = int(self.headers.get("Content-Length") or 0)
        if n > 20000:
            return None
        try:
            return json.loads(self.rfile.read(n) or b"{}")
        except Exception:
            return None

    def _ip(self):
        return self.headers.get("X-Forwarded-For", self.client_address[0]).split(",")[0].strip()

    def log_message(self, fmt, *args):
        pass

    def do_GET(self):
        calls_reset()
        if not allowed(self._ip()):
            return self._send(429, {"status": "error", "reason": "too many requests, slow down"})
        path = self.path.split("?")[0]
        if path == "/api/cabinets/state":
            session("_probe")
            return self._send(200, {"status": "success", "deals": core.DEALS, "fields": core.FIELDS, "cabinets": {k: {"program": p, "sha": core.sha(p), "expert": "cabinet_" + k} for k, p in core.CABINET_PROGRAMS.items()},
                                    "default_rules": core.DEFAULT_RULES, "devices": {k: {"name": v["name"], "label": v["label"]} for k, v in DEVICES.items()},
                                    "model": MODEL if OPENROUTER_KEY else None, "generations_left_today": max(0, DAY_LIMIT - STATE["generations_today"]), "handler_sha": registrar_sha(), "language": "cabinet + policy"})
        if path.startswith("/api/cabinets/export/"):
            key = path.split("/")[4]
            if key not in core.CABINET_PROGRAMS:
                return self._send(400, {"status": "error", "reason": "unknown cabinet"})
            try:
                registrar = open(os.path.join(HERE, "demo_register_cabinet.fython"), encoding="utf-8").read()
            except OSError:
                return self._send(500, {"status": "error", "reason": "handler file is missing on the server"})
            return self._send(200, {"format": "extella-expert-bundle/1", "handler_sha": registrar_sha(), "exported_at": time.strftime("%Y-%m-%d %H:%M UTC", time.gmtime()), "from": "https://extella.dev/cabinets/",
                                    "verify": "take the handler text between the triple quotes, delete the line starting with HANDLER_SHA, sha256 the rest; evaluations report the same value as handler_sha",
                                    "experts": [{"name": "demo_register_cabinet", "cspl": "fython", "code": registrar, "kwargs": {}, "description": "Registers the cabinet language handler on the device it runs on. Run once per device.", "role": "language handler (run once on your device)"},
                                                {"name": "cabinet_" + key, "cspl": "cabinet", "code": json.dumps(core.CABINET_PROGRAMS[key], ensure_ascii=False, indent=1), "kwargs": {}, "description": "The %s cabinet: its own checks." % key, "role": "the expert (a cabinet program); pass rules and deals as params"}],
                                    "how_to": ["save both experts into your account: POST https://api.extella.ai/api/expert/save with headers X-Auth-Token, X-Profile-Id: default, X-Agent-Id",
                                               "run demo_register_cabinet once with targets = [your device id]",
                                               "run cabinet_" + key + " with params {rules: [...policy rules...], deals: [...]} and the same targets",
                                               "or simply: python3 take_home.py https://extella.dev/api/cabinets/export/" + key + " --level render"]})
        return self._send(404, {"status": "error", "reason": "unknown path"})

    def do_POST(self):
        calls_reset()
        if not allowed(self._ip()):
            return self._send(429, {"status": "error", "reason": "too many requests, slow down"})
        path = self.path.split("?")[0]
        body = self._body()
        if body is None:
            return self._send(400, {"status": "error", "reason": "bad json"})
        sid = str(body.get("session", ""))[:64] or self._ip()
        device = str(body.get("device") or "vps")
        if device not in DEVICES:
            return self._send(400, {"status": "error", "reason": "unknown device"})

        if path == "/api/cabinets/evaluate":
            s = session(sid)
            with LOCK:
                rules = [json.loads(json.dumps(r)) for r in s["rules"]]
            t0 = time.time()
            results = evaluate_all(rules, device, force=bool(body.get("force")))
            ok = all(r.get("status") == "success" for r in results.values())
            first = next((r for r in results.values() if r.get("status") == "success"), None)
            return self._send(200, {"status": "success" if ok else "failed", "rules": rules, "rules_sha": core.sha(rules), "device": device, "seconds": round(time.time() - t0, 1), "results": results,
                                    "host": first and first.get("host"), "os": first and first.get("os"), "handler_sha": first and first.get("handler_sha"), "handler_sha_expected": registrar_sha(),
                                    "model_calls_measured": calls_count(), "history": s["history"], "reason": None if ok else next((r.get("reason") for r in results.values() if r.get("status") != "success"), None)})

        if path == "/api/cabinets/rule":
            wish = str(body.get("wish", "")).strip()[:200]
            scope = [x for x in (body.get("scope") or CABINETS) if x in CABINETS] or CABINETS
            if len(wish) < 3:
                return self._send(400, {"status": "error", "reason": "write the rule in a sentence"})
            if not OPENROUTER_KEY:
                return self._send(503, {"status": "error", "reason": "generation is not configured on this server"})
            refusal = take_generation(sid)
            if refusal:
                return self._send(403, {"status": "error", "reason": refusal})
            steps = []
            t0 = time.time()
            try:
                rule, meta = generate_rule(wish, scope)
            except Exception as err:
                steps.append({"step": "generate", "ok": False, "seconds": round(time.time() - t0, 1), "detail": "model call failed: " + str(err)[:120]})
                return self._send(200, {"status": "failed", "steps": steps})
            steps.append({"step": "generate", "ok": True, "seconds": meta["seconds"], "detail": "%s · %s tokens" % (meta["model"], meta["tokens"])})
            if isinstance(rule, dict) and body.get("scope"):
                rule["scope"] = scope        # the user's checkboxes win over the model's guess
            out = add_rule_path(rule, sid, wish=wish, steps=steps, device=device)
            if out["status"] != "success" and len(out["steps"]) > 1 and out["steps"][1]["step"] == "grammar" and not out["steps"][1]["ok"]:
                try:
                    rule2, meta2 = generate_rule(wish, scope, rule, out["steps"][1]["detail"])
                    if isinstance(rule2, dict) and body.get("scope"):
                        rule2["scope"] = scope
                    steps2 = [{"step": "generate", "ok": True, "seconds": meta2["seconds"], "detail": "second attempt · %s tokens" % meta2["tokens"]}]
                    out2 = add_rule_path(rule2, sid, wish=wish, steps=steps2, device=device)
                    out2["steps"] = out["steps"] + out2["steps"]
                    out = out2
                except Exception as err:
                    out["steps"].append({"step": "generate", "ok": False, "seconds": 0, "detail": "second attempt failed: " + str(err)[:120]})
            out["wish"] = wish
            out["model_calls_create"] = calls_count()
            out["handler_sha_expected"] = registrar_sha()
            return self._send(200, out)

        if path == "/api/cabinets/rule-json":
            rule = body.get("rule")
            if not isinstance(rule, dict):
                return self._send(400, {"status": "error", "reason": "rule must be an object"})
            out = add_rule_path(rule, sid, wish=str(body.get("wish", ""))[:200], device=device)
            out["model_calls_create"] = 0
            return self._send(200, out)

        if path == "/api/cabinets/rule/remove":
            s = session(sid)
            rid = str(body.get("id", ""))
            with LOCK:
                s["rules"] = [r for r in s["rules"] if r["id"] != rid]
                rules = [json.loads(json.dumps(r)) for r in s["rules"]]
            results = evaluate_all(rules, device)
            return self._send(200, {"status": "success", "rules": rules, "rules_sha": core.sha(rules), "results": results, "device": device})

        if path == "/api/cabinets/reset":
            s = session(sid)
            with LOCK:
                s["rules"] = [json.loads(json.dumps(r)) for r in core.DEFAULT_RULES]
                s["history"] = []
                rules = [json.loads(json.dumps(r)) for r in s["rules"]]
            results = evaluate_all(rules, device)
            return self._send(200, {"status": "success", "rules": rules, "rules_sha": core.sha(rules), "results": results, "device": device})

        return self._send(404, {"status": "error", "reason": "unknown path"})


def warm_up():
    while True:
        for device in ("vps", "second"):
            try:
                evaluate_all([json.loads(json.dumps(r)) for r in core.DEFAULT_RULES], device)
            except Exception as err:
                sys.stderr.write("warm-up %s failed: %s\n" % (device, str(err)[:120]))
        time.sleep(1800)


if __name__ == "__main__":
    print("cabinets proxy on 127.0.0.1:%d, model %s" % (PORT, MODEL if OPENROUTER_KEY else "none"))
    threading.Thread(target=warm_up, daemon=True).start()
    ThreadingHTTPServer(("127.0.0.1", PORT), Handler).serve_forever()
