# Chat Surface — Starlark Backend (v0.1.0)
#
# Thin surface layer on top of chat-core library.
# Only handles typing indicator broadcast — all CRUD is
# delegated to chat-core's own API routes.
#
# Modules: json, realtime

def _resp(status, data):
    return {"status": status, "body": json.encode(data), "headers": {"Content-Type": "application/json"}}


def on_request(req):
    path   = req["path"]
    method = req["method"]
    user_id = req.get("user_id", "")

    # POST /typing/:conversation_id — broadcast typing indicator
    if method == "POST" and path.startswith("/typing/"):
        cid = path[len("/typing/"):]
        if not cid:
            return _resp(400, {"error": "conversation_id required"})

        body = json.decode(req.get("body", "{}"))
        display_name = str(body.get("display_name", ""))

        realtime.publish(
            "conversation:" + cid,
            "typing",
            {
                "participant_id": user_id,
                "display_name": display_name,
                "conversation_id": cid,
            },
        )

        return _resp(200, {"ok": True})

    return _resp(404, {"error": "not found"})
