{
  "openapi": "3.1.0",
  "info": {
    "title": "sword drill API",
    "version": "1.0.0",
    "description": "Internal API for the sword drill Bible trivia app. Endpoints are implemented as Supabase Edge Functions and require a valid user JWT unless otherwise noted. This document is published for transparency and for AI agents reasoning about the system.",
    "contact": { "name": "sword drill", "url": "https://www.sworddrilltrivia.com" },
    "license": { "name": "Proprietary" }
  },
  "servers": [
    { "url": "https://bhfagqucxxualacwenjm.supabase.co/functions/v1", "description": "Production edge functions" }
  ],
  "tags": [
    { "name": "quiz", "description": "Quiz session and answer endpoints" },
    { "name": "leaderboard", "description": "Leaderboard data" },
    { "name": "onboarding", "description": "Onboarding and placement" },
    { "name": "stats", "description": "User statistics" }
  ],
  "components": {
    "securitySchemes": {
      "userJwt": { "type": "http", "scheme": "bearer", "bearerFormat": "JWT" }
    },
    "schemas": {
      "QuizStartRequest": {
        "type": "object",
        "properties": {
          "mode": { "type": "string", "enum": ["daily", "custom", "h2h", "placement"] },
          "difficulty": { "type": "string", "enum": ["new_to_story", "sunday_school", "seminary"] },
          "book": { "type": "string" },
          "theme": { "type": "string" }
        },
        "required": ["mode"]
      },
      "QuizStartResponse": {
        "type": "object",
        "properties": {
          "session_id": { "type": "string", "format": "uuid" },
          "questions": { "type": "array", "items": { "$ref": "#/components/schemas/Question" } }
        }
      },
      "Question": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "prompt": { "type": "string" },
          "choices": { "type": "array", "items": { "type": "string" } },
          "difficulty": { "type": "string" },
          "book": { "type": "string" }
        }
      },
      "QuizAnswerRequest": {
        "type": "object",
        "properties": {
          "session_id": { "type": "string", "format": "uuid" },
          "question_id": { "type": "string", "format": "uuid" },
          "choice_index": { "type": "integer" },
          "elapsed_ms": { "type": "integer" }
        },
        "required": ["session_id", "question_id", "choice_index"]
      },
      "QuizAnswerResponse": {
        "type": "object",
        "properties": {
          "correct": { "type": "boolean" },
          "correct_index": { "type": "integer" },
          "explanation": { "type": "string" },
          "points_awarded": { "type": "integer" }
        }
      },
      "LeaderboardRow": {
        "type": "object",
        "properties": {
          "rank": { "type": "integer" },
          "username": { "type": "string" },
          "points": { "type": "integer" },
          "level": { "type": "string" }
        }
      }
    }
  },
  "security": [{ "userJwt": [] }],
  "paths": {
    "/quiz-start": {
      "post": {
        "tags": ["quiz"],
        "summary": "Start a quiz session",
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/QuizStartRequest" } } }
        },
        "responses": {
          "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/QuizStartResponse" } } } }
        }
      }
    },
    "/quiz-answer": {
      "post": {
        "tags": ["quiz"],
        "summary": "Submit a quiz answer",
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/QuizAnswerRequest" } } }
        },
        "responses": {
          "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/QuizAnswerResponse" } } } }
        }
      }
    },
    "/leaderboard": {
      "get": {
        "tags": ["leaderboard"],
        "summary": "Fetch leaderboard rows",
        "parameters": [
          { "name": "scope", "in": "query", "schema": { "type": "string", "enum": ["global", "friends", "weekly"] } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 50 } }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": { "application/json": { "schema": { "type": "array", "items": { "$ref": "#/components/schemas/LeaderboardRow" } } } }
          }
        }
      }
    },
    "/placement-quiz": {
      "post": {
        "tags": ["onboarding"],
        "summary": "Run a placement quiz to suggest a knowledge level",
        "responses": { "200": { "description": "OK" } }
      }
    },
    "/update-stats": {
      "post": {
        "tags": ["stats"],
        "summary": "Recompute the authenticated user's stats",
        "responses": { "200": { "description": "OK" } }
      }
    }
  }
}
