{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.examark.net/reference/examark-paper-v1.schema.json",
  "title": "Examark Paper v1 Semantic Draft",
  "description": "The normalized semantic structure produced by compiling an Examark Markdown v1 paper.",
  "type": "object",
  "additionalProperties": false,
  "required": ["format", "version", "paper", "print", "groups"],
  "properties": {
    "format": { "const": "examark-paper" },
    "version": { "const": 1 },
    "paper": {
      "type": "object",
      "additionalProperties": false,
      "required": ["title"],
      "properties": { "title": { "type": "string", "minLength": 1, "maxLength": 200 } }
    },
    "print": { "$ref": "#/$defs/printConfig" },
    "groups": {
      "type": "array",
      "minItems": 1,
      "maxItems": 50,
      "items": { "$ref": "#/$defs/group" }
    }
  },
  "$defs": {
    "key": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9_.:-]{0,127}$" },
    "questionType": {
      "enum": ["single_choice", "multiple_choice", "true_false", "fill_blank", "subjective", "composition"]
    },
    "printConfig": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "paper_main_title": { "type": "string" },
        "show_student_info": { "type": "boolean" },
        "show_instructions": { "type": "boolean" },
        "show_exam_time": { "type": "boolean" },
        "exam_time": { "type": "string" },
        "show_exam_location": { "type": "boolean" },
        "exam_location": { "type": "string" },
        "show_exam_duration": { "type": "boolean" },
        "exam_duration": { "type": "string" },
        "show_manual_grading_table": { "type": "boolean" },
        "show_page_numbers": { "type": "boolean" },
        "show_question_count": { "type": "boolean" },
        "show_total_score": { "type": "boolean" },
        "body_font_size_pt": { "type": "number", "minimum": 9, "maximum": 14 },
        "instructions": { "type": "string" },
        "student_no_config": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "prefix": { "type": "string" },
            "digit_count": { "type": "integer", "minimum": 1 },
            "digits": { "type": "string" }
          }
        },
        "answer_sheet_config": {
          "type": "object",
          "additionalProperties": false,
          "properties": { "enhanced_scanning": { "type": "boolean" } }
        }
      }
    },
    "group": {
      "type": "object",
      "additionalProperties": false,
      "required": ["key", "title", "allowed_types", "default_scores", "items"],
      "properties": {
        "key": { "$ref": "#/$defs/key" },
        "title": { "type": "string", "minLength": 1, "maxLength": 200 },
        "allowed_types": {
          "type": "array",
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/questionType" }
        },
        "default_scores": {
          "type": "object",
          "additionalProperties": { "type": "number", "minimum": 0, "maximum": 1000 }
        },
        "items": {
          "type": "array",
          "maxItems": 500,
          "items": { "oneOf": [{ "$ref": "#/$defs/instruction" }, { "$ref": "#/$defs/question" }] }
        }
      }
    },
    "instruction": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "key", "content"],
      "properties": {
        "kind": { "const": "instruction" },
        "key": { "$ref": "#/$defs/key" },
        "content": { "type": "string", "maxLength": 100000 }
      }
    },
    "option": {
      "type": "object",
      "additionalProperties": false,
      "required": ["label", "content"],
      "properties": {
        "label": { "type": "string", "minLength": 1, "maxLength": 20 },
        "content": { "type": "string", "maxLength": 100000 }
      }
    },
    "blank": {
      "type": "object",
      "additionalProperties": false,
      "required": ["key", "score", "answers"],
      "properties": {
        "key": { "$ref": "#/$defs/key" },
        "score": { "type": "number", "minimum": 0, "maximum": 1000 },
        "answers": { "type": "array", "items": { "type": "string" } }
      }
    },
    "question": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "key", "type", "score", "stem"],
      "properties": {
        "kind": { "const": "question" },
        "key": { "$ref": "#/$defs/key" },
        "type": { "$ref": "#/$defs/questionType" },
        "score": { "type": "number", "minimum": 0, "maximum": 1000 },
        "stem": { "type": "string", "maxLength": 100000 },
        "options": { "type": "array", "maxItems": 6, "items": { "$ref": "#/$defs/option" } },
        "answer": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
        "blanks": { "type": "array", "items": { "$ref": "#/$defs/blank" } },
        "answer_style": { "enum": ["lined", "blank"] },
        "answer_lines": { "type": "integer", "minimum": 1 },
        "composition_rows": { "type": "integer", "minimum": 1 },
        "rubric": { "type": "string", "maxLength": 100000 },
        "explanation": { "type": "string", "maxLength": 100000 }
      },
      "allOf": [
        {
          "if": { "properties": { "type": { "enum": ["single_choice", "multiple_choice", "true_false"] } } },
          "then": { "required": ["options", "answer"] }
        },
        {
          "if": { "properties": { "type": { "const": "fill_blank" } } },
          "then": { "required": ["blanks", "rubric"] }
        },
        {
          "if": { "properties": { "type": { "const": "subjective" } } },
          "then": { "required": ["answer_style", "answer_lines", "rubric"] }
        },
        {
          "if": { "properties": { "type": { "const": "composition" } } },
          "then": { "required": ["composition_rows", "rubric"] }
        }
      ]
    }
  }
}
