{
  "openapi": "3.1.0",
  "info": {
    "title": "Hypha organizer API",
    "version": "1.0.0",
    "summary": "Plan a pickleball session, preview its lineup, and confirm creation.",
    "description": "This is the private organizer pilot. Use the origin of the Hypha deployment provided by the operator. A server-issued bearer grant binds calls to one existing workspace. Planning and preview grants can request new session options and lineups. Only confirmation-enabled grants can create a private thread and session. Drafts and confirmed creation receipts are scoped to their workspace. Workspace IDs, browser cookies and Guest links do not authorize these API calls. The API surface is limited to planning, preview and confirmed creation. It offers no endpoints to list or read private sessions or submit scores. Any future keyless access is outside this pilot.\n\nSend JSON POST requests of at most 16,384 bytes. Per workspace, planning and preview share 60 attempts per rolling minute. Confirmation allows 20 attempts per rolling 15 minutes. Failed inputs and idempotent retries can consume quota. On 429, wait at least the number of seconds in `Retry-After` before retrying. Use the same draft if a confirmation is retried."
  },
  "servers": [
    {
      "url": "/",
      "description": "The HTTPS Hypha deployment that served this document."
    }
  ],
  "security": [
    {
      "PilotBearer": []
    }
  ],
  "paths": {
    "/api/agent/v1/lineup/plan": {
      "post": {
        "operationId": "suggestSessionFormats",
        "summary": "Suggest feasible session formats",
        "description": "Returns format options and rotation tradeoffs for a copied roster or expected attendance. If courts are unknown, the response can suggest how many courts to confirm before previewing. Options identify whether the agent can preview and create them or whether they require the Hypha web app. An empty options array is valid when input constraints or a named unsupported format rule out a recommendation. This operation only writes rate-limit bookkeeping.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PlanRequest"
              },
              "examples": {
                "synthetic": {
                  "summary": "Thirteen players on three courts",
                  "value": {
                    "playerCount": 13,
                    "courts": 3,
                    "durationMinutes": 90,
                    "intent": "Rotate partners and keep waiting low."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Format options, any clarifying question, and a next step.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PlanResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "413": {
            "$ref": "#/components/responses/TooLarge"
          },
          "415": {
            "$ref": "#/components/responses/UnsupportedMediaType"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "500": {
            "$ref": "#/components/responses/ServerError"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/api/agent/v1/lineup/preview": {
      "post": {
        "operationId": "previewLineup",
        "summary": "Preview a lineup for a named roster",
        "description": "Use rotating-partner doubles round robin or court ladder. Round robin returns the scheduled rounds and ladder returns its opening round. Hypha seals the reviewed lineup into a workspace-bound draft that can create a new session for 15 minutes. Preview does not persist the lineup, create a session, or issue a Guest link. Rate-limit bookkeeping still applies.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PreviewRequest"
              },
              "examples": {
                "synthetic": {
                  "summary": "Opening ladder on two courts",
                  "value": {
                    "roster": "Demo Player 1\nDemo Player 2\nDemo Player 3\nDemo Player 4\nDemo Player 5\nDemo Player 6\nDemo Player 7\nDemo Player 8",
                    "courts": 2,
                    "format": "ladder"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The lineup, opaque draft, and expiry.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PreviewResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "413": {
            "$ref": "#/components/responses/TooLarge"
          },
          "415": {
            "$ref": "#/components/responses/UnsupportedMediaType"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "500": {
            "$ref": "#/components/responses/ServerError"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/api/agent/v1/lineup/confirm": {
      "post": {
        "operationId": "confirmLineup",
        "summary": "Create the approved private session",
        "description": "Requires a confirmation-enabled grant and the exact draft approved by the organizer. The caller obtains the organizer’s approval before sending `confirmed: true`. Creation is idempotent for that workspace and draft. The same draft can recover the same receipt after a timeout, even after its creation window expires, while the original Guest invitation remains available. A revoked or replaced invitation is not regenerated by retry. The pilot returns a Guest invitation and separate passcode. Guest access to the session follows Hypha’s existing Guest permissions.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ConfirmRequest"
              },
              "examples": {
                "synthetic": {
                  "summary": "Illustrative draft placeholder",
                  "description": "Replace the synthetic placeholder with the exact draft from preview after reviewing it with the organizer.",
                  "value": {
                    "draft": "SYNTHETIC_PREVIEW_DRAFT",
                    "confirmed": true
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The new session and thread IDs with the Guest link and its separate passcode, or the same receipt on retry.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ConfirmResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/InvitationUnavailable"
          },
          "409": {
            "$ref": "#/components/responses/DraftConflict"
          },
          "413": {
            "$ref": "#/components/responses/TooLarge"
          },
          "415": {
            "$ref": "#/components/responses/UnsupportedMediaType"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "500": {
            "$ref": "#/components/responses/ServerError"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "PilotBearer": {
        "type": "http",
        "scheme": "bearer",
        "description": "Obtain the pilot bearer privately from the deployment operator. The server assigns its workspace and whether it permits confirmation."
      }
    },
    "responses": {
      "BadRequest": {
        "description": "A required field is missing, roster or court constraints failed, the draft is invalid, or the request contains unsupported fields.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Unauthorized": {
        "description": "A valid pilot bearer grant is required. Missing, invalid and revoked grants use this same response.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Forbidden": {
        "description": "The grant is limited to planning and preview, or the draft belongs to another workspace.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "InvitationUnavailable": {
        "description": "The original confirmed Guest invitation is unavailable. A retry cannot create a replacement.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "DraftConflict": {
        "description": "The unused draft expired or can no longer reproduce its original lineup. Review a fresh preview before confirming.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "TooLarge": {
        "description": "The JSON request exceeds 16,384 bytes.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "UnsupportedMediaType": {
        "description": "Use Content-Type application/json.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "RateLimited": {
        "description": "The workspace exceeded its quota for this operation.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        },
        "headers": {
          "Retry-After": {
            "required": true,
            "description": "Minimum wait in whole seconds before retrying.",
            "schema": {
              "type": "integer",
              "minimum": 1
            },
            "example": 25
          }
        }
      },
      "ServerError": {
        "description": "Hypha could not complete the request. Use the same draft to retry confirmation.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Unavailable": {
        "description": "The deployment, required configuration or Guest passcode service is temporarily unavailable.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    },
    "schemas": {
      "PlanRequest": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "roster": {
            "type": "string",
            "minLength": 1,
            "maxLength": 8192,
            "description": "Paste 1–48 distinct player labels, one per line. Each label must be 1–40 characters. Use distinct labels for players who share a name."
          },
          "playerCount": {
            "type": "integer",
            "minimum": 1,
            "maximum": 48,
            "description": "Expected attendance. If roster is also supplied, the count must equal its number of players."
          },
          "courts": {
            "type": "integer",
            "minimum": 1,
            "maximum": 24,
            "description": "Available courts. Omit this field if unknown. Options can use fewer courts."
          },
          "durationMinutes": {
            "type": "integer",
            "minimum": 1,
            "maximum": 1440,
            "description": "Total booking length in minutes. This is advisory."
          },
          "intent": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200,
            "description": "Organizer goals such as rotating partners or reducing waiting."
          }
        },
        "anyOf": [
          {
            "required": [
              "roster"
            ]
          },
          {
            "required": [
              "playerCount"
            ]
          }
        ]
      },
      "PreviewRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "roster",
          "courts",
          "format"
        ],
        "properties": {
          "roster": {
            "type": "string",
            "minLength": 1,
            "maxLength": 8192,
            "description": "Paste 4–48 distinct player labels, one per line. Each label must be 1–40 characters. Use distinct labels for players who share a name."
          },
          "courts": {
            "type": "integer",
            "minimum": 1,
            "maximum": 12,
            "description": "Doubles courts for this lineup. At least four players are required for each requested court. When planning first, use the chosen option’s courtsUsed."
          },
          "format": {
            "$ref": "#/components/schemas/PreviewFormat"
          }
        }
      },
      "ConfirmRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "draft",
          "confirmed"
        ],
        "properties": {
          "draft": {
            "$ref": "#/components/schemas/Draft"
          },
          "confirmed": {
            "const": true,
            "type": "boolean",
            "description": "The organizer explicitly approved this exact reviewed draft."
          }
        }
      },
      "PlanResult": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "playerCount",
          "requestedCourts",
          "options",
          "unsupported",
          "clarifyingQuestion",
          "nextStep"
        ],
        "properties": {
          "playerCount": {
            "type": "integer",
            "minimum": 1,
            "maximum": 48
          },
          "requestedCourts": {
            "type": [
              "integer",
              "null"
            ],
            "minimum": 1,
            "maximum": 24,
            "description": "Available courts supplied in the request or null when unknown."
          },
          "options": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/PlanOption"
            }
          },
          "unsupported": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/UnsupportedFormat"
            },
            "description": "Unsupported formats explicitly requested by the organizer."
          },
          "clarifyingQuestion": {
            "type": [
              "string",
              "null"
            ]
          },
          "nextStep": {
            "type": "string"
          }
        }
      },
      "PlanOption": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "format",
          "label",
          "description",
          "agentSupport",
          "rosterLimits",
          "courtLimits",
          "courtsUsed",
          "playersPerCourt",
          "activePlayers",
          "idlePlayers",
          "rotation",
          "waveBasis",
          "waves"
        ],
        "properties": {
          "format": {
            "type": "string",
            "enum": [
              "round-robin",
              "ladder",
              "open-play-doubles",
              "open-play-singles",
              "singles-round-robin",
              "fixed-team-round-robin",
              "tournament-fixed-doubles",
              "tournament-singles"
            ]
          },
          "label": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "agentSupport": {
            "type": "string",
            "enum": [
              "preview_and_create",
              "web_only"
            ]
          },
          "rosterLimits": {
            "$ref": "#/components/schemas/Limits"
          },
          "courtLimits": {
            "$ref": "#/components/schemas/Limits"
          },
          "courtsUsed": {
            "type": "integer",
            "minimum": 1,
            "maximum": 24
          },
          "playersPerCourt": {
            "type": "integer",
            "enum": [
              2,
              4
            ]
          },
          "activePlayers": {
            "type": "integer",
            "minimum": 0,
            "maximum": 48
          },
          "idlePlayers": {
            "type": "integer",
            "minimum": 0,
            "maximum": 48
          },
          "rotation": {
            "type": "string"
          },
          "waveBasis": {
            "type": "string",
            "enum": [
              "scheduled",
              "opening",
              "one-game"
            ],
            "description": "Scheduled draw, initial stage that depends on results later, or one manual game."
          },
          "waves": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/WaveRange"
            },
            "description": "Consecutive waves with the same court and player counts may be grouped into an inclusive range."
          }
        }
      },
      "Limits": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "min",
          "max"
        ],
        "properties": {
          "min": {
            "type": "integer",
            "minimum": 1
          },
          "max": {
            "type": "integer",
            "minimum": 1
          }
        }
      },
      "WaveRange": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "wave",
          "lastWave",
          "courtsUsed",
          "activePlayers",
          "idlePlayers"
        ],
        "properties": {
          "wave": {
            "type": "integer",
            "minimum": 1,
            "description": "First wave in this range."
          },
          "lastWave": {
            "type": "integer",
            "minimum": 1,
            "description": "Last wave in this range, inclusive."
          },
          "courtsUsed": {
            "type": "integer",
            "minimum": 1,
            "maximum": 24
          },
          "activePlayers": {
            "type": "integer",
            "minimum": 0,
            "maximum": 48
          },
          "idlePlayers": {
            "type": "integer",
            "minimum": 0,
            "maximum": 48
          }
        }
      },
      "UnsupportedFormat": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "format",
          "label",
          "reason"
        ],
        "properties": {
          "format": {
            "type": "string"
          },
          "label": {
            "type": "string"
          },
          "reason": {
            "type": "string"
          }
        }
      },
      "PreviewResult": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "draft",
          "expiresAt",
          "lineup"
        ],
        "properties": {
          "draft": {
            "$ref": "#/components/schemas/Draft"
          },
          "expiresAt": {
            "type": "string",
            "format": "date-time",
            "description": "UTC time after which this unused draft cannot create a session."
          },
          "lineup": {
            "$ref": "#/components/schemas/Lineup"
          }
        }
      },
      "Draft": {
        "type": "string",
        "minLength": 1,
        "maxLength": 12000,
        "pattern": "^[A-Za-z0-9_-]+$",
        "description": "Opaque workspace-scoped draft returned by preview. Preserve it exactly."
      },
      "PreviewFormat": {
        "type": "string",
        "enum": [
          "round-robin",
          "ladder"
        ]
      },
      "Lineup": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "format",
          "title",
          "date",
          "courts",
          "players",
          "rounds"
        ],
        "properties": {
          "format": {
            "$ref": "#/components/schemas/PreviewFormat"
          },
          "title": {
            "type": "string"
          },
          "date": {
            "type": "string",
            "format": "date"
          },
          "courts": {
            "type": "integer",
            "minimum": 1,
            "maximum": 12
          },
          "players": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1,
              "maxLength": 40
            },
            "minItems": 4,
            "maxItems": 48
          },
          "rounds": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/LineupRound"
            }
          }
        }
      },
      "LineupRound": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "round",
          "games",
          "waiting"
        ],
        "properties": {
          "round": {
            "type": "integer",
            "minimum": 1
          },
          "games": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/LineupGame"
            }
          },
          "waiting": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1,
              "maxLength": 40
            }
          }
        }
      },
      "LineupGame": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "court",
          "teamA",
          "teamB"
        ],
        "properties": {
          "court": {
            "type": "integer",
            "minimum": 1,
            "maximum": 12
          },
          "teamA": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1,
              "maxLength": 40
            },
            "minItems": 2,
            "maxItems": 2
          },
          "teamB": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1,
              "maxLength": 40
            },
            "minItems": 2,
            "maxItems": 2
          }
        }
      },
      "ConfirmResult": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "threadId",
          "sessionId",
          "guestUrl",
          "guestPasscode"
        ],
        "properties": {
          "threadId": {
            "type": "string",
            "format": "uuid"
          },
          "sessionId": {
            "type": "string",
            "format": "uuid"
          },
          "guestUrl": {
            "type": "string",
            "format": "uri",
            "description": "Guest invitation at the configured public Hypha app origin. Its private ticket is in the URL fragment."
          },
          "guestPasscode": {
            "type": "string",
            "pattern": "^[0-9]{4}$",
            "description": "Four digits, supplied separately from the link. Preserve leading zeroes."
          }
        }
      },
      "Error": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "string",
            "description": "Human-readable description."
          }
        }
      }
    }
  }
}
