{
  "openapi": "3.1.0",
  "info": {
    "title": "Mindforge API",
    "version": "1.0.0",
    "description": "Public API for Mindforge, a multiplayer trivia roguelike. Use these endpoints to discover the next public run, create or join a room, then upgrade to the WebSocket endpoint to play. Game state is pushed to clients as personalized `state` snapshots; clients send small command messages. There is no account system: `POST /api/rooms` issues a per-seat `token` that authorizes the WebSocket connection and reconnects.",
    "contact": { "name": "Mindforge", "url": "https://mindforge.alexandrucuriman.com/developers", "email": "contact@alexandrucuriman.com" },
    "license": { "name": "Proprietary" }
  },
  "servers": [{ "url": "https://mindforge.alexandrucuriman.com", "description": "Production" }],
  "tags": [
    { "name": "meta", "description": "Service metadata." },
    { "name": "rooms", "description": "Room discovery and matchmaking." },
    { "name": "realtime", "description": "Live game play over WebSocket." }
  ],
  "paths": {
    "/api/health": {
      "get": {
        "tags": ["meta"],
        "operationId": "getHealth",
        "summary": "Liveness check",
        "description": "Returns a static status payload. Use it to verify the backend is reachable before joining a room.",
        "responses": {
          "200": {
            "description": "Service is healthy.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["status"],
                  "properties": { "status": { "type": "string", "enum": ["ok"] } },
                  "additionalProperties": false
                },
                "example": { "status": "ok" }
              }
            }
          }
        }
      }
    },
    "/api/public": {
      "get": {
        "tags": ["rooms"],
        "operationId": "getPublicRoom",
        "summary": "Current public room",
        "description": "Returns the room that is currently filling for the next public run. A new public room opens every 45 seconds and starts automatically when `startsAt` is reached. Compare `startsAt` with `serverNow` (not your local clock) to time a join.",
        "responses": {
          "200": {
            "description": "The public room accepting players right now.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["code", "startsAt", "serverNow", "players"],
                  "properties": {
                    "code": { "type": "string", "pattern": "^[0-9A-F]{6}$", "description": "Six-character room code." },
                    "startsAt": { "type": "integer", "format": "int64", "description": "Unix timestamp (milliseconds) when the join window closes and the run starts." },
                    "serverNow": { "type": "integer", "format": "int64", "description": "Current server time (Unix milliseconds) for clock offset correction." },
                    "players": { "type": "integer", "minimum": 0, "description": "Seats currently taken in the room." }
                  },
                  "additionalProperties": false
                },
                "example": { "code": "A1B2C3", "startsAt": 1758700000000, "serverNow": 1758699985000, "players": 7 }
              }
            }
          },
          "404": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/rooms": {
      "post": {
        "tags": ["rooms"],
        "operationId": "joinRoom",
        "summary": "Create or join a room",
        "description": "Joins the current public run (`mode: \"public\"`), creates a private room (`mode: \"create\"` or an empty `code`), or joins a private lobby by its six-character `code`. The response `token` is a per-seat credential: pass it to `/api/ws` and keep it secret. Sending the same `token` again reclaims the seat instead of creating a duplicate player. Late joining after a run starts is rejected with 404 or 409.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "mode": { "type": "string", "enum": ["public", "create", "join"], "description": "`public` joins the scheduled run; `create` opens a private room; `join` (or a bare `code`) joins an existing lobby." },
                  "code": { "type": "string", "maxLength": 6, "description": "Six-character room code for private joins. Ignored for `public`." },
                  "locale": { "type": "string", "enum": ["en", "fr"], "default": "en", "description": "Language for this player's questions and interface." },
                  "name": { "type": "string", "maxLength": 24, "description": "Display name; blank assigns a random name. Control characters are rejected." },
                  "token": { "type": "string", "description": "Seat token from an earlier join; reclaims that seat on reconnect." }
                },
                "additionalProperties": false
              },
              "examples": {
                "public": { "value": { "mode": "public" } },
                "create": { "value": { "mode": "create", "name": "Ada", "locale": "en" } },
                "join": { "value": { "code": "A1B2C3" } }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Seat reserved. Connect to `/api/ws` with `code` and `token`.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["code", "playerId", "token", "name"],
                  "properties": {
                    "code": { "type": "string", "pattern": "^[0-9A-F]{6}$" },
                    "playerId": { "type": "string", "description": "Stable player identifier inside the room." },
                    "token": { "type": "string", "description": "Secret seat credential for the WebSocket." },
                    "name": { "type": "string", "description": "Effective display name (server-generated when blank)." }
                  },
                  "additionalProperties": false
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/Error" },
          "403": { "$ref": "#/components/responses/Error" },
          "404": { "$ref": "#/components/responses/Error" },
          "409": { "$ref": "#/components/responses/Error" },
          "410": { "$ref": "#/components/responses/Error" },
          "503": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/ws": {
      "get": {
        "tags": ["realtime"],
        "operationId": "openGameSocket",
        "summary": "Live game WebSocket",
        "description": "Upgrades to a WebSocket (`ws://`/`wss://`) bound to one seat. Requires `code` and `token` from `POST /api/rooms`; treat the token as a password. The server sends personalized `{\"type\":\"state\", ...}` snapshots (phase, question, deadline, players, your card offer) and `{\"type\":\"error\",\"message\":...}` frames. Clients send command messages: `{\"type\":\"start\"}` (private-room host), `{\"type\":\"choose_card\",\"draft\":-1,\"card\":\"steady\"}`, `{\"type\":\"answer\",\"question\":0,\"choice\":2}`, `{\"type\":\"buy_life\"}`, `{\"type\":\"set_locale\",\"locale\":\"fr\"}`. Reconnecting with the same token replaces the connection and restores the seat.",
        "parameters": [
          { "name": "code", "in": "query", "required": true, "schema": { "type": "string", "pattern": "^[0-9A-F]{6}$" }, "description": "Room code from the join response." },
          { "name": "token", "in": "query", "required": true, "schema": { "type": "string" }, "description": "Seat token from the join response." }
        ],
        "responses": {
          "101": { "description": "WebSocket upgrade accepted; state snapshots follow." },
          "401": { "$ref": "#/components/responses/Error" },
          "404": { "$ref": "#/components/responses/Error" }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Error": {
        "type": "object",
        "required": ["error", "code", "hint"],
        "properties": {
          "error": { "type": "string", "description": "Human-readable message." },
          "code": { "type": "string", "description": "Stable machine-readable code such as `not_found` or `method_not_allowed`." },
          "hint": { "type": "string", "description": "How to resolve the error or where to find the API contract." }
        },
        "additionalProperties": false
      }
    },
    "responses": {
      "Error": {
        "description": "Structured JSON error.",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" },
            "example": { "error": "Unknown API endpoint.", "code": "not_found", "hint": "See /openapi.json for the published API contract." }
          }
        }
      }
    }
  }
}
