{
  "openapi": "3.1.0",
  "info": {
    "title": "MarkScheme AI API",
    "version": "1.0.0",
    "description": "REST API for Edexcel IAL Economics (WEC11-WEC14) answer upload, asynchronous AO1-AO4 grading, Teacher Workspace batch/student management, and credit tracking.",
    "contact": {
      "name": "MarkScheme Support",
      "url": "https://mark-scheme.com/contact"
    }
  },
  "servers": [
    {
      "url": "https://app.mark-scheme.com",
      "description": "Authenticated Teacher & Student Workspace Proxy"
    },
    {
      "url": "https://hgbloxw6e7gq3y734xcia5cj640wyizo.lambda-url.us-east-1.on.aws",
      "description": "Presigned S3 Upload Service"
    },
    {
      "url": "https://ufg5m3qhqcqkbbafbms4viaoma0ljbbf.lambda-url.us-east-1.on.aws",
      "description": "Grading Results Polling Service"
    }
  ],
  "security": [
    {
      "BearerAuth": []
    }
  ],
  "paths": {
    "/api/teacher/overview": {
      "get": {
        "operationId": "getTeacherOverview",
        "summary": "Get teacher workspace overview",
        "description": "Returns the authenticated teacher profile, class batches, recent activity, and remaining grading credits.",
        "responses": {
          "200": {
            "description": "Teacher overview payload",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TeacherOverview"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          }
        }
      }
    },
    "/api/teacher/credits": {
      "get": {
        "operationId": "getTeacherCredits",
        "summary": "Get remaining teacher grading credits",
        "description": "Returns the teacher's remaining credit balance out of the default 10-credit allocation.",
        "responses": {
          "200": {
            "description": "Credit balance payload",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreditsResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          }
        }
      }
    },
    "/api/teacher/batches": {
      "get": {
        "operationId": "listTeacherBatches",
        "summary": "List teacher class batches",
        "description": "Returns all active and archived class batches owned by the authenticated teacher.",
        "responses": {
          "200": {
            "description": "List of batches",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BatchListResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          }
        }
      },
      "post": {
        "operationId": "createTeacherBatch",
        "summary": "Create a class batch",
        "description": "Creates a new class batch in the teacher workspace.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateBatchRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Created batch",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Batch"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequestError"
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          }
        }
      }
    },
    "/api/teacher/batches/{batch_id}/students": {
      "get": {
        "operationId": "listBatchStudents",
        "summary": "List students in a batch",
        "description": "Returns paginated student roster entries for the specified batch.",
        "parameters": [
          {
            "$ref": "#/components/parameters/BatchId"
          },
          {
            "name": "limit",
            "in": "query",
            "description": "Maximum number of student entries to return per page.",
            "schema": {
              "type": "integer",
              "default": 50
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "description": "Opaque pagination cursor returned from the previous list request.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Paginated student roster",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/StudentListResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          }
        }
      },
      "post": {
        "operationId": "createBatchStudent",
        "summary": "Add a student to a batch",
        "description": "Creates a managed student account and links it to the specified batch.",
        "parameters": [
          {
            "$ref": "#/components/parameters/BatchId"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateStudentRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Created student credentials",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/StudentCredentialResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequestError"
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          }
        }
      }
    },
    "/api/teacher/batches/{batch_id}/submissions": {
      "get": {
        "operationId": "listBatchSubmissions",
        "summary": "List submissions in a batch",
        "description": "Returns graded student or ad-hoc submissions for the specified batch (use batch_id=adhoc for teacher ad-hoc checks).",
        "parameters": [
          {
            "$ref": "#/components/parameters/BatchId"
          }
        ],
        "responses": {
          "200": {
            "description": "Submission list",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SubmissionListResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          }
        }
      },
      "post": {
        "operationId": "linkBatchSubmission",
        "summary": "Link a submission to a batch",
        "description": "Links an uploaded submission ID to a class batch or the reserved adhoc batch.",
        "parameters": [
          {
            "$ref": "#/components/parameters/BatchId"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/LinkSubmissionRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Linked submission confirmation",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SubmissionLinkResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequestError"
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          }
        }
      }
    },
    "/api/teacher/my-batches": {
      "get": {
        "operationId": "listStudentBatches",
        "summary": "List batches for signed-in student",
        "description": "Returns class memberships for a student authenticated with a Cognito JWT or STU_ session token.",
        "responses": {
          "200": {
            "description": "Student memberships",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/StudentBatchesResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          }
        }
      }
    },
    "/api/teacher/my-work/{batch_id}": {
      "get": {
        "operationId": "listStudentBatchWork",
        "summary": "List student submissions in a batch",
        "description": "Returns the authenticated student's graded submissions and teacher feedback in the given batch.",
        "parameters": [
          {
            "$ref": "#/components/parameters/BatchId"
          }
        ],
        "responses": {
          "200": {
            "description": "Student submissions",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/StudentWorkResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT or STU_ token",
        "description": "Cognito JWT (teachers/students) or STU_ session token (teacher-managed students). See https://mark-scheme.com/auth.md."
      }
    },
    "parameters": {
      "BatchId": {
        "name": "batch_id",
        "in": "path",
        "required": true,
        "description": "Unique batch identifier or 'adhoc' for teacher ad-hoc practice checks.",
        "schema": {
          "type": "string"
        }
      }
    },
    "schemas": {
      "TeacherOverview": {
        "type": "object",
        "description": "Overview of the authenticated teacher's workspace including remaining grading credits and owned class batches.",
        "properties": {
          "creditsRemaining": {
            "type": "integer",
            "description": "Number of grading credits left out of the teacher's allocation."
          },
          "batches": {
            "type": "array",
            "description": "Class batches belonging to the teacher.",
            "items": {
              "$ref": "#/components/schemas/Batch"
            }
          }
        }
      },
      "CreditsResponse": {
        "type": "object",
        "description": "Teacher credit balance response object.",
        "required": ["creditsRemaining"],
        "properties": {
          "creditsRemaining": {
            "type": "integer",
            "minimum": 0,
            "description": "Remaining grading credits available to the teacher."
          }
        }
      },
      "Batch": {
        "type": "object",
        "description": "Represents a teacher class batch for organizing students, assignments, and submissions.",
        "properties": {
          "batchId": {
            "type": "string",
            "description": "Unique identifier for the class batch."
          },
          "name": {
            "type": "string",
            "description": "Human-readable class or cohort name."
          },
          "archived": {
            "type": "boolean",
            "description": "Whether the batch is archived (read-only) or active."
          }
        }
      },
      "BatchListResponse": {
        "type": "object",
        "description": "List response containing all batches owned by the teacher.",
        "properties": {
          "batches": {
            "type": "array",
            "description": "Collection of teacher batch summaries.",
            "items": {
              "$ref": "#/components/schemas/Batch"
            }
          }
        }
      },
      "CreateBatchRequest": {
        "type": "object",
        "description": "Request body to create a new class batch in the teacher workspace.",
        "required": ["name"],
        "properties": {
          "name": {
            "type": "string",
            "description": "Display name of the new class batch."
          },
          "unit": {
            "type": "string",
            "enum": ["WEC11", "WEC12", "WEC13", "WEC14"],
            "description": "Edexcel IAL Economics unit code associated with this batch."
          }
        }
      },
      "StudentEntry": {
        "type": "object",
        "description": "Roster record for a student enrolled in a teacher batch.",
        "properties": {
          "studentId": {
            "type": "string",
            "description": "Unique student identifier within the batch."
          },
          "name": {
            "type": "string",
            "description": "Student full name."
          },
          "username": {
            "type": "string",
            "description": "Managed sign-in username issued to the student."
          }
        }
      },
      "StudentListResponse": {
        "type": "object",
        "description": "Paginated list of students belonging to a batch.",
        "properties": {
          "items": {
            "type": "array",
            "description": "Array of student roster entries.",
            "items": {
              "$ref": "#/components/schemas/StudentEntry"
            }
          },
          "cursor": {
            "type": ["string", "null"],
            "description": "Pagination cursor for retrieving the next page, or null when complete."
          }
        }
      },
      "CreateStudentRequest": {
        "type": "object",
        "description": "Payload to provision a student account inside a batch.",
        "required": ["name"],
        "properties": {
          "name": {
            "type": "string",
            "description": "Student display name."
          },
          "email": {
            "type": "string",
            "format": "email",
            "description": "Optional student email address for notifications."
          }
        }
      },
      "StudentCredentialResponse": {
        "type": "object",
        "description": "Newly created student account details including generated username and initial password.",
        "properties": {
          "student": {
            "$ref": "#/components/schemas/StudentEntry"
          },
          "password": {
            "type": "string",
            "description": "Initial generated sign-in password for the student."
          }
        }
      },
      "SubmissionSummary": {
        "type": "object",
        "description": "Graded or pending answer submission record with AO1-AO4 marks and teacher review overrides.",
        "properties": {
          "submissionId": {
            "type": "string",
            "description": "Unique submission identifier."
          },
          "studentId": {
            "type": "string",
            "description": "Identifier of the submitting student, if assigned."
          },
          "status": {
            "type": "string",
            "description": "Current grading status (e.g., pending, completed, irrelevant_upload)."
          }
        }
      },
      "SubmissionListResponse": {
        "type": "object",
        "description": "List of submissions belonging to a batch or ad-hoc workspace.",
        "properties": {
          "items": {
            "type": "array",
            "description": "Collection of submission summaries.",
            "items": {
              "$ref": "#/components/schemas/SubmissionSummary"
            }
          }
        }
      },
      "LinkSubmissionRequest": {
        "type": "object",
        "description": "Request payload linking an uploaded S3 submission ID into a batch and optional student/assignment.",
        "required": ["submissionId"],
        "properties": {
          "submissionId": {
            "type": "string",
            "description": "Identifier returned by the upload URL generator."
          },
          "studentId": {
            "type": "string",
            "description": "Optional target student identifier."
          },
          "assignmentId": {
            "type": "string",
            "description": "Optional target assignment identifier."
          }
        }
      },
      "SubmissionLinkResponse": {
        "type": "object",
        "description": "Confirmation payload returned after linking a submission to a batch.",
        "properties": {
          "linked": {
            "type": "boolean",
            "description": "True when the submission was successfully linked."
          },
          "submission": {
            "$ref": "#/components/schemas/SubmissionSummary"
          }
        }
      },
      "StudentBatchesResponse": {
        "type": "object",
        "description": "Class batch memberships visible to the authenticated student.",
        "properties": {
          "batches": {
            "type": "array",
            "description": "Batches the student is enrolled in.",
            "items": {
              "$ref": "#/components/schemas/Batch"
            }
          }
        }
      },
      "StudentWorkResponse": {
        "type": "object",
        "description": "Graded submissions and teacher feedback visible to the authenticated student for a batch.",
        "properties": {
          "items": {
            "type": "array",
            "description": "Student submissions in the requested batch.",
            "items": {
              "$ref": "#/components/schemas/SubmissionSummary"
            }
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "description": "Structured API error payload containing a human-readable message, machine code, and remediation hint.",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "string",
            "description": "Human-readable explanation of why the request failed."
          },
          "code": {
            "type": "string",
            "description": "Stable machine-readable error identifier."
          },
          "remediation": {
            "type": "string",
            "description": "Actionable guidance describing how the client or agent can fix the error."
          }
        }
      }
    },
    "responses": {
      "UnauthorizedError": {
        "description": "Missing or invalid Bearer token",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "BadRequestError": {
        "description": "Invalid request parameters or unsupported file/source-code payload",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      }
    }
  }
}
