#!/usr/bin/env python3
"""Blocks demo proxy: a small site made of block experts, rendered by real handlers on two devices.

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

  GET  /api/blocks/state                  the site's blocks (sources + sha), devices, laws, model
  POST /api/blocks/render                 {device: vps|mac, law: ""|vat|ru, blocks?: [ids], force?: bool} -> rendered fragments per block, css, host
  POST /api/blocks/teach                  {wish, session} -> the model writes a block, grammar check, expert saved, rendered on both devices
  POST /api/blocks/teach-block            {block, session} -> same path without the model
  POST /api/blocks/share                  {skill, session, note} -> visitors' strip (library) + per-block API key
  GET  /api/blocks/library                blocks left by visitors
  GET  /api/blocks/export/<skill>[?handler=1|2|3]   bundle: the chosen handler (default v1) + the block, as JSON
"""
import hashlib
import json
import os
import secrets
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 block_core as core  # noqa: E402
import blocks2_api as b2  # noqa: E402
try:
    import live_api  # noqa: E402  the live coffee site (demo/live), copied here by demo/live/deploy.sh
except ImportError:
    live_api = None
try:
    import traffic_api  # noqa: E402  rush hour (demo/traffic), copied here by demo/traffic/deploy.sh
except ImportError:
    traffic_api = None

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"}}
# A language handler is a registration on a device. Device and handler version are two separate axes: when a visitor asks
# for a version the device does not hold, the proxy re-registers the handler there (one platform call, ~10 s) before rendering.
HANDLERS = {1: "v1 · editorial", 2: "v2 · compact dark", 3: "v3 · plain text"}   # v1, v2: one HTML renderer, two looks; v3: separate code, text output
HANDLER_START = {"vps": 1, "second": 2, "mac": 2}     # what each device holds when the proxy starts (deploy.sh registers these)
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("BLOCKS_PORT", "18451"))
DAY_LIMIT = int(os.environ.get("ROBOT_DAY_LIMIT", "3000"))
SESSION_LIMIT = int(os.environ.get("ROBOT_SESSION_LIMIT", "10"))
LIBRARY_KEY = "blocks_library"
HERE = os.path.dirname(os.path.abspath(__file__))
SITE = ["skill_block_hero", "skill_block_features", "skill_block_price", "skill_block_quote", "skill_block_faq", "skill_block_cta"]
LAWS = {"vat": {"en": "prices shown with 12% VAT", "ru": "цены с НДС 12%"}, "ru": {"en": "interface labels in Russian", "ru": "подписи интерфейса по-русски"}}

LOCK = threading.Lock()
STATE = {"day": time.strftime("%Y-%m-%d"), "generations_today": 0, "sessions": {}, "rate": {}, "sources": {}, "cache": {},
         "handler_on": dict(HANDLER_START), "handler_at": {}}
DEV_LOCK = {k: threading.RLock() for k in DEVICES}
CALLS = threading.local()
POOL = ThreadPoolExecutor(max_workers=8)


def calls_reset():
    CALLS.n = 0


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


# ---------------------------------------------------------------- platform

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), "device": dev["name"]}
    except Exception as e:
        return {"status": "error", "reason": "platform did not answer: " + str(e)[:120], "platform_seconds": round(time.time() - t0, 1), "device": dev["name"]}
    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)
    res["device"] = res.get("host") or dev["name"]
    res["device_key"] = device
    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 get_source(expert):
    """The block's JSON as stored on the platform (cached; the sha proves it did not change between renders)."""
    with LOCK:
        cached = STATE["sources"].get(expert)
    if cached:
        return cached
    try:
        ex = platform("/expert/get", {"name": expert}, timeout=30)
        doc = json.loads(ex.get("expert_code") or "null")
    except Exception:
        return None
    if not isinstance(doc, dict):
        return None
    entry = {"id": expert, "block": doc, "sha": core.source_sha(doc), "type": doc.get("type")}
    with LOCK:
        STATE["sources"][expert] = entry
    return entry


def save_block(name, block, description):
    out = platform("/expert/save", {"name": name, "description": description[:300], "code": json.dumps(block, ensure_ascii=False, indent=1),
                                    "cspl": "block", "kwargs": {"mode": "render"}, "global": False}, timeout=60)
    if out.get("status") == "success":
        with LOCK:
            STATE["sources"][name] = {"id": name, "block": block, "sha": core.source_sha(block), "type": block.get("type")}
    return out


def kv_get(key):
    try:
        out = platform("/kv/get", {"key": key}, timeout=30)
    except urllib.error.HTTPError:
        return None
    val = out.get("value") if isinstance(out, dict) else None
    if isinstance(val, str):
        try:
            return json.loads(val)
        except Exception:
            return None
    return val


def kv_set(key, value, description=""):
    return platform("/kv/set", {"key": key, "value": json.dumps(value, ensure_ascii=False), "description": description}, timeout=30)


def registrar_sha(version):
    try:
        reg = open(os.path.join(HERE, "demo_register_block_v%d.fython" % version), 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


# ---------------------------------------------------------------- rendering with a cache

def ensure_handler(device, version):
    """Make the device hold handler <version> of the block language; re-register it there if it holds another one.
    Returns {"changed": bool, "seconds": float, "status": ..., "reason": ...}. Caller holds DEV_LOCK[device]."""
    if STATE["handler_on"].get(device) == version:
        return {"changed": False, "seconds": 0.0, "status": "success"}
    r = run_expert("demo_register_block_v%d" % version, {}, device)
    if r.get("status") != "success" or int(r.get("version") or r.get("handler_version") or 0) != version:
        return {"changed": False, "seconds": r.get("platform_seconds"), "status": "error", "reason": r.get("reason") or "handler did not register"}
    STATE["handler_on"][device] = version
    STATE["handler_at"][device] = time.strftime("%H:%M:%S")
    return {"changed": True, "seconds": r.get("platform_seconds"), "status": "success", "host": r.get("host") or r.get("device")}


def all_stored(ids, device, law, version):
    """True when every requested block has a stored result for this device, law and handler version: nothing needs the device."""
    for sid in ids:
        src = get_source(sid)
        if not src or (sid, device, law or "", version, src["sha"]) not in STATE["cache"]:
            return False
    return True


def render_blocks(ids, device, law, force=False, version=None):
    """Render every block on the device, in parallel. Cache key includes the handler version and the source sha."""
    results = {}
    todo = []
    version = version or STATE["handler_on"].get(device, 1)
    for sid in ids:
        src = get_source(sid)
        if not src:
            results[sid] = {"status": "error", "reason": "no such block"}
            continue
        key = (sid, device, law or "", version, src["sha"])
        with LOCK:
            hit = None if force else STATE["cache"].get(key)
        if hit:
            r = dict(hit)
            r["cached"] = True
            results[sid] = r
        else:
            todo.append((sid, key))
    if todo:
        params = {"mode": "render"}
        if law:
            params["law"] = law
        futs = {POOL.submit(run_expert, sid, params, device): (sid, key) for sid, key in todo}
        for fut, (sid, key) in futs.items():
            r = fut.result()
            r["cached"] = False
            r["rendered_at"] = time.strftime("%H:%M:%S")
            r["rendered_date"] = time.strftime("%Y-%m-%d")
            if r.get("status") == "success":
                with LOCK:
                    STATE["cache"][key] = r
            results[sid] = r
    return results


# ---------------------------------------------------------------- generation

SYSTEM_PROMPT = """You write ONE block for a small website in the block language. Output ONLY a JSON object, nothing else.
Types and fields (plain text only, no markup, no links, no emoji):
{"type": "hero", "title": <=80 chars, "text": <=240, "button": <=40}
{"type": "features", "title": <=80, "items": [ {"title": <=60, "text": <=160} ] (1-4 items)}
{"type": "price", "title": <=80, "currency": "USD"|"EUR"|"KZT"|"RUB", "plans": [ {"name": <=40, "price": number, "period": "month"|"year"|"once", "features": [<=80 chars each, up to 6]} ] (1-3 plans)}
{"type": "faq", "title": <=80, "items": [ {"q": <=120, "a": <=300} ] (1-5 items)}
{"type": "quote", "text": <=300, "author": <=60, "role": <=60, "rating": 1-5}
{"type": "cta", "title": <=80, "text": <=200, "button": <=40}
{"type": "text", "title": <=80, "text": <=600}
The site is a small coffee subscription called Northwind Coffee. Write real, specific copy in the language of the wish. Use ONLY these fields.
Examples:
"a customer review with five stars" -> {"type": "quote", "text": "Switched from supermarket beans in March. Have not looked back.", "author": "Ilya M.", "role": "home barista", "rating": 5}
"three reasons to choose us" -> {"type": "features", "title": "Three reasons", "items": [{"title": "Fresh", "text": "Roasted the week it ships."}, {"title": "Honest", "text": "Farm and roast date on every bag."}, {"title": "Simple", "text": "Change anything by writing a sentence."}]}
"""


def generate_block(wish, previous=None, failure=None):
    user = "Wish: " + wish
    if previous is not None:
        user += "\nYour previous block was rejected: " + str(failure)[:300] + "\nPrevious block: " + json.dumps(previous, ensure_ascii=False) + "\nFix it using only the grammar."
    body = {"model": MODEL, "temperature": 0.4, "max_tokens": 500, "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/blocks/", "X-Title": "Extella blocks 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 skill_id(block, session):
    return "skill_block_" + hashlib.sha256((json.dumps(block, sort_keys=True, ensure_ascii=False) + "|" + session).encode()).hexdigest()[:10]


# ---------------------------------------------------------------- limits & sessions

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


# Costly requests call a device or a model and hold the server for 10-30 s. The per-minute limit above does not stop
# a few addresses from keeping the server busy all day, so costly requests also have a daily limit per address and
# a limit on how many run at once for everyone; a request that finds no free slot within COSTLY_WAIT seconds is refused.
COSTLY_PER_DAY = int(os.environ.get("COSTLY_PER_DAY", "150"))
COSTLY_AT_ONCE = int(os.environ.get("COSTLY_AT_ONCE", "6"))
COSTLY_WAIT = 25
COSTLY_SLOTS = threading.BoundedSemaphore(COSTLY_AT_ONCE)


def costly_take(ip):
    """None if this address may make one more costly request today (and counts it), else the refusal in words."""
    day = time.strftime("%Y-%m-%d")
    with LOCK:
        if STATE.get("costly_day") != day:
            STATE["costly_day"], STATE["costly"], STATE["rate"] = day, {}, {}
        n = STATE["costly"].get(ip, 0)
        if n >= COSTLY_PER_DAY:
            return "this address has used its %d server runs for today; the count starts again at 00:00 UTC" % COSTLY_PER_DAY
        STATE["costly"][ip] = n + 1
    return None


def costly_refund(ip):
    with LOCK:
        if STATE.get("costly", {}).get(ip):
            STATE["costly"][ip] -= 1


CHEAP_POST = ("/api/blocks/share",)   # POST paths that touch no device and no model


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, "blocks": []})


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 teach_path(block, sid, wish="", steps=None):
    steps = steps if steps is not None else []
    ok, reason = core.validate(block)
    steps.append({"step": "grammar", "ok": ok, "seconds": 0.0, "detail": "every field is in the block language; strings only, no markup" if ok else reason})
    if not ok:
        return {"status": "failed", "steps": steps, "block": block}
    t0 = time.time()
    ok, report = core.check_block(block)
    steps.append({"step": "test_render", "ok": ok, "seconds": round(time.time() - t0, 2), "detail": "6 renders (2 handlers x 3 laws), no script survives escaping" if ok else report})
    name = skill_id(block, sid)
    t0 = time.time()
    out = save_block(name, block, "Block taught on the demo site. Wish: %s" % wish[:150])
    ok = out.get("status") == "success"
    steps.append({"step": "save_expert", "ok": ok, "seconds": round(time.time() - t0, 1), "detail": name if ok else json.dumps(out)[:200]})
    if not ok:
        return {"status": "failed", "steps": steps, "block": block}
    renders = {}
    for dev, ver in (("vps", 1), ("second", 2)):              # the new block in both handler versions, one per server
        with DEV_LOCK[dev]:
            reg = ensure_handler(dev, ver)
            r = render_blocks([name], dev, None, force=True, version=ver)[name] if reg.get("status") == "success" else {"status": "error", "reason": reg.get("reason")}
        renders[dev] = r
        steps.append({"step": "render_" + dev, "ok": r.get("status") == "success", "seconds": r.get("platform_seconds"), "detail": ("%s · handler v%s · %d bytes" % (r.get("host"), r.get("handler_version"), len(r.get("html", "")))) if r.get("status") == "success" else r.get("reason")})
    s = session(sid)
    with LOCK:
        if name not in s["blocks"]:
            s["blocks"].append(name)
    return {"status": "success" if renders["vps"].get("status") == "success" else "failed", "skill": name, "block": block, "sha": core.source_sha(block), "renders": renders, "steps": steps}


# ---------------------------------------------------------------- http

class Handler(BaseHTTPRequestHandler):
    server_version = "extella-blocks/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/blocks/state":
            session("_probe")
            blocks = [get_source(s) for s in SITE]
            return self._send(200, {"status": "success", "site": [b for b in blocks if b], "devices": {k: {"name": v["name"], "label": v["label"], "handler_on": STATE["handler_on"].get(k), "handler": HANDLERS.get(STATE["handler_on"].get(k), "")} for k, v in DEVICES.items()},
                                    "handlers": {str(k): v for k, v in HANDLERS.items()}, "laws": LAWS, "model": MODEL if OPENROUTER_KEY else None, "generations_left_today": max(0, DAY_LIMIT - STATE["generations_today"]),
                                    "handler_sha": {"1": registrar_sha(1), "2": registrar_sha(2), "3": registrar_sha(3)}, "language": "block"})
        if path == "/api/blocks/site/state":
            law = kv_get(b2.LAW_KEY) or {"version": "nolaw"}
            return self._send(200, {"status": "success", "site": "northwind.coffee", "law": dict(law, label=b2.LAW_VERSIONS.get(law.get("version"), "")),
                                    "laws": b2.LAW_VERSIONS, "blocks": b2.all_blocks(kv_get), "examples": b2.EXAMPLES,
                                    "models": {k: b2.WRITER_NAMES[k] for k, v in b2.WRITERS.items() if v},
                                    "generations_left_today": max(0, DAY_LIMIT - STATE["generations_today"])})
        if path == "/api/blocks/traffic/mayors" and traffic_api:
            q = self.path.split("?", 1)[1] if "?" in self.path else ""
            lvl = next((x.split("=", 1)[1] for x in q.split("&") if x.startswith("level=")), "school")[:20]
            return self._send(200, traffic_api.mayors(kv_get, lvl))
        if path == "/api/blocks/live/state" and live_api:
            return self._send(200, live_api.state(platform, lambda n, p: run_expert(n, p, "vps"), kv_get, kv_set))
        if path == "/api/blocks/library":
            lib = kv_get(LIBRARY_KEY) or []
            return self._send(200, {"status": "success", "blocks": [{k: v for k, v in s.items() if k != "key_hash"} for s in lib]})
        if path.startswith("/api/blocks/export/"):
            sid = path.split("/")[4]
            if not sid.startswith("skill_block_"):
                return self._send(400, {"status": "error", "reason": "unknown block"})
            src = get_source(sid)
            if not src:
                return self._send(404, {"status": "error", "reason": "no such block"})
            q = self.path.split("?", 1)[1] if "?" in self.path else ""
            hv = next((int(x.split("=", 1)[1]) for x in q.split("&") if x.startswith("handler=") and x.split("=", 1)[1] in ("1", "2", "3")), 1)
            reg_name = "demo_register_block_v%d" % hv
            try:
                registrar = open(os.path.join(HERE, reg_name + ".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(hv), "handler": HANDLERS[hv], "exported_at": time.strftime("%Y-%m-%d %H:%M UTC", time.gmtime()), "from": "https://extella.dev/blocks/",
                                    "verify": "take the handler text between the triple quotes, delete the line starting with HANDLER_SHA, sha256 the rest; renders report the same value as handler_sha",
                                    "experts": [{"name": reg_name, "cspl": "fython", "code": registrar, "kwargs": {}, "description": "Registers the block language handler %s on the device it runs on. Run once per device." % HANDLERS[hv], "role": "language handler (run once on your device)"},
                                                {"name": sid, "cspl": "block", "code": json.dumps(src["block"], ensure_ascii=False, indent=1), "kwargs": {"mode": "render"}, "description": "A block of the demo site.", "role": "the expert (a block document)"}],
                                    "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 " + reg_name + " once with targets = [your device id]",
                                               "run " + sid + " with params {mode: render, law: vat} and the same targets; the result carries html, css, host and handler_sha",
                                               "or simply: python3 take_home.py https://extella.dev/api/blocks/export/" + sid + " --level render"]})
        return self._send(404, {"status": "error", "reason": "unknown path"})

    def do_POST(self):
        calls_reset()
        ip = self._ip()
        if not allowed(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"})
        if path in CHEAP_POST:
            return self._post(path, body)
        refusal = costly_take(ip)
        if refusal:
            return self._send(429, {"status": "limit", "reason": refusal})
        if not COSTLY_SLOTS.acquire(timeout=COSTLY_WAIT):
            costly_refund(ip)
            return self._send(503, {"status": "busy", "reason": "the server is busy with other visitors' runs; try again in a minute"})
        try:
            return self._post(path, body)
        finally:
            COSTLY_SLOTS.release()

    def _post(self, path, body):

        if path == "/api/blocks/render":
            device = str(body.get("device") or "vps")
            if device not in DEVICES:
                return self._send(400, {"status": "error", "reason": "unknown device"})
            law = body.get("law") or None
            if law is not None and law not in LAWS:
                return self._send(400, {"status": "error", "reason": "unknown law"})
            ids = body.get("blocks")
            if not isinstance(ids, list) or not ids:
                ids = list(SITE)
            ids = [str(x) for x in ids if str(x).startswith("skill_block_")][:12]
            try:
                version = int(body.get("handler") or STATE["handler_on"].get(device, 1))
            except (TypeError, ValueError):
                version = 0
            if version not in HANDLERS:
                return self._send(400, {"status": "error", "reason": "handler must be 1, 2 or 3"})
            t0 = time.time()
            with DEV_LOCK[device]:
                # a stored result needs no device call, so the handler is not re-registered for it
                reg = {"changed": False, "seconds": 0.0, "status": "success"} if (not body.get("force") and all_stored(ids, device, law, version)) else ensure_handler(device, version)
                if reg.get("status") != "success":
                    return self._send(200, {"status": "failed", "device": device, "law": law, "handler": version, "registered": reg, "seconds": round(time.time() - t0, 1), "blocks": [],
                                            "reason": reg.get("reason") or "could not register the handler"})
                results = render_blocks(ids, device, law, force=bool(body.get("force")), version=version)
            first = next((r for r in results.values() if r.get("status") == "success"), None)
            return self._send(200, {"status": "success" if first else "failed", "device": device, "law": law, "handler": version, "registered": reg, "seconds": round(time.time() - t0, 1),
                                    "rendered_at": first and first.get("rendered_at"), "rendered_date": first and first.get("rendered_date"), "all_cached": bool(results) and all(r.get("cached") for r in results.values()),
                                    "host": first and first.get("host"), "os": first and first.get("os"), "handler_version": first and first.get("handler_version"), "handler_name": first and first.get("handler_name"),
                                    "handler_sha": first and first.get("handler_sha"), "handler_sha_expected": registrar_sha(first.get("handler_version")) if first else None,
                                    "css": first and first.get("css"), "model_calls_measured": calls_count(),
                                    "blocks": [{"id": sid, "status": r.get("status"), "html": r.get("html"), "type": r.get("block_type"), "text": r.get("text"), "source_sha": r.get("source_sha"), "cached": r.get("cached"), "rendered_at": r.get("rendered_at"), "seconds": r.get("platform_seconds"), "reason": r.get("reason")} for sid, r in ((x, results[x]) for x in ids if x in results)],   # in the order asked, not cached-first
                                    "reason": None if first else next((r.get("reason") for r in results.values()), "no render")})

        if path == "/api/blocks/site/render":
            res = b2.render_site(lambda n, p: run_expert(n, p, "vps"), kv_get)
            res["model_calls_measured"] = calls_count()
            return self._send(200, res)

        if path == "/api/blocks/site/teach":
            wish = str(body.get("wish", "")).strip()[:200]
            sid = str(body.get("session", ""))[:64] or self._ip()
            model = "large" if body.get("model") == "large" else "small"
            if len(wish) < 3:
                return self._send(400, {"status": "error", "reason": "say what the block should say, in a sentence"})
            if not b2.WRITERS.get(model):
                return self._send(503, {"status": "error", "reason": "this model is not configured on the server"})
            refusal = take_generation(sid)
            if refusal:
                return self._send(403, {"status": "no_ai", "reason": refusal})
            def count():
                CALLS.n = calls_count() + 1
            def save2(name, text, desc):
                return platform("/expert/save", {"name": name, "description": desc[:300], "code": text, "cspl": "block2", "kwargs": {"mode": "render"}, "global": False}, timeout=60)
            out = b2.teach(lambda p, b, timeout=90: platform(p, b, timeout=timeout), lambda n, p: run_expert(n, p, "vps"), save2, count, kv_get, kv_set, wish, sid, model)
            out["model_calls_create"] = calls_count()
            return self._send(200, out)

        if path == "/api/blocks/traffic/score" and traffic_api:
            sid = str(body.get("session", ""))[:64] or self._ip()
            return self._send(200, traffic_api.score(platform, lambda n, p: run_expert(n, p, "vps"), kv_get, kv_set,
                                                     str(body.get("level", ""))[:20], body.get("rules"), sid))

        if path == "/api/blocks/live/refresh" and live_api:
            return self._send(200, live_api.refresh(platform, lambda n, p: run_expert(n, p, "vps"), kv_get, kv_set))

        if path == "/api/blocks/live/teach" and live_api:
            wish = str(body.get("wish", "")).strip()[:200]
            sid = str(body.get("session", ""))[:64] or self._ip()
            model = "large" if body.get("model") == "large" else "small"
            if len(wish) < 3:
                return self._send(400, {"status": "error", "reason": "say in a sentence how the site should change"})
            refusal = take_generation(sid)
            if refusal:
                return self._send(403, {"status": "no_ai", "reason": refusal})
            return self._send(200, live_api.teach(platform, lambda n, p: run_expert(n, p, "vps"), kv_get, kv_set, wish, sid, model))

        if path == "/api/blocks/live/law" and live_api:
            return self._send(200, live_api.change_law(platform, lambda n, p: run_expert(n, p, "vps"), kv_get, kv_set, str(body.get("version", ""))))

        if path == "/api/blocks/site/law":
            out = b2.change_law(lambda n, p: run_expert(n, p, "vps"), kv_get, kv_set, str(body.get("version", "")))
            out["model_calls_measured"] = calls_count()
            return self._send(200, out)

        if path == "/api/blocks/teach":
            wish = str(body.get("wish", "")).strip()[:240]
            sid = str(body.get("session", ""))[:64] or self._ip()
            if len(wish) < 3:
                return self._send(400, {"status": "error", "reason": "describe the block 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:
                block, meta = generate_block(wish)
            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"])})
            out = teach_path(block, sid, wish=wish, steps=steps)
            if out["status"] != "success" and len(out["steps"]) > 1 and out["steps"][1]["step"] == "grammar" and not out["steps"][1]["ok"]:
                try:
                    block2, meta2 = generate_block(wish, block, out["steps"][1]["detail"])
                    steps2 = [{"step": "generate", "ok": True, "seconds": meta2["seconds"], "detail": "second attempt · %s tokens" % meta2["tokens"]}]
                    out2 = teach_path(block2, sid, wish=wish, steps=steps2)
                    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()
            return self._send(200, out)

        if path == "/api/blocks/teach-block":
            block = body.get("block")
            sid = str(body.get("session", ""))[:64] or self._ip()
            if not isinstance(block, dict):
                return self._send(400, {"status": "error", "reason": "block must be an object"})
            out = teach_path(block, sid, wish=str(body.get("wish", ""))[:200])
            out["model_calls_create"] = 0
            return self._send(200, out)

        if path == "/api/blocks/share":
            name = str(body.get("skill", ""))
            src = get_source(name) if name.startswith("skill_block_") and name not in SITE else None
            if not src:
                return self._send(400, {"status": "error", "reason": "teach your own block first"})
            key = secrets.token_urlsafe(18)
            entry = {"id": name, "type": src["type"], "block": src["block"], "sha": src["sha"], "note": str(body.get("note", ""))[:140], "wish": str(body.get("wish", ""))[:200],
                     "created": time.strftime("%Y-%m-%d"), "key_hash": hashlib.sha256(key.encode()).hexdigest()}
            with LOCK:
                lib = kv_get(LIBRARY_KEY) or []
                if any(s["id"] == name for s in lib):
                    return self._send(409, {"status": "error", "reason": "already shared"})
                lib.append(entry)
                kv_set(LIBRARY_KEY, lib[-100:], "Blocks demo: blocks left by visitors")
            return self._send(200, {"status": "success", "skill": name, "api_key": key, "api": {"method": "POST", "path": "/api/blocks/%s/render" % name, "header": "X-Robot-Key: <key>", "body": {"device": "vps", "law": "vat"}}})

        if path.startswith("/api/blocks/skill_block_") and path.endswith("/render"):
            name = path.split("/")[3]
            key = self.headers.get("X-Robot-Key", "")
            lib = kv_get(LIBRARY_KEY) or []
            entry = next((s for s in lib if s["id"] == name), None)
            if not entry or hashlib.sha256(key.encode()).hexdigest() != entry.get("key_hash"):
                return self._send(403, {"status": "error", "reason": "bad block or key"})
            device = str(body.get("device") or "vps")
            law = body.get("law") or None
            if device not in DEVICES or (law is not None and law not in LAWS):
                return self._send(400, {"status": "error", "reason": "device vps|mac, law vat|ru"})
            with DEV_LOCK[device]:
                r = render_blocks([name], device, law, force=True)[name]
            r["via"] = "api"
            return self._send(200, r)

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


def probe_handlers():
    """Ask each always-on device which handler version it really holds (one render of the hero block), so the proxy
    never guesses after a restart: the registration lives on the device, not in this process."""
    for device in ("vps", "second"):
        with DEV_LOCK[device]:
            r = run_expert(SITE[0], {"mode": "render"}, device)
            if r.get("status") == "success" and r.get("handler_version"):
                STATE["handler_on"][device] = int(r["handler_version"])
                STATE["handler_at"][device] = time.strftime("%H:%M:%S")
            else:
                sys.stderr.write("probe %s failed: %s\n" % (device, r.get("reason")))


def warm_up():
    """Render the six blocks on both devices for every law once at start and every 30 minutes, so visitors see
    the site at once; the cache is marked as such and the 'Render again, live' button always runs for real."""
    probe_handlers()
    while True:
        for device in ("vps", "second"):
            for law in (None, "vat", "ru"):
                try:
                    with DEV_LOCK[device]:
                        render_blocks(list(SITE), device, law, force=False)
                except Exception as err:
                    sys.stderr.write("warm-up %s/%s failed: %s\n" % (device, law, str(err)[:120]))
        time.sleep(1800)


if __name__ == "__main__":
    print("blocks 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()
