{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.examark.net/reference/examark-paper-v2.schema.json",
  "title": "Examark Paper v2 Semantic Draft",
  "description": "Normalized semantic structure produced by compiling Examark Markdown v2. Cross-field score sums are additionally enforced by the Examark compiler.",
  "type": "object",
  "additionalProperties": false,
  "required": ["format", "version", "paper", "print", "groups"],
  "properties": {
    "format": { "const": "examark-paper" },
    "version": { "const": 2 },
    "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}$"
    },
    "rubricKey": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9_.:-]{0,63}$"
    },
    "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", "exclusiveMinimum": 0, "maximum": 1000 },
        "answers": {
          "type": "array",
          "minItems": 1,
          "maxItems": 20,
          "items": { "type": "string", "minLength": 1, "maxLength": 1000 }
        }
      }
    },
    "rubricLevel": {
      "type": "object",
      "additionalProperties": false,
      "required": ["score", "description"],
      "properties": {
        "score": { "type": "number", "minimum": 0, "maximum": 1000 },
        "description": { "type": "string", "minLength": 1, "maxLength": 2000 }
      }
    },
    "rubricCriterion": {
      "type": "object",
      "additionalProperties": false,
      "required": ["key", "title", "points", "observable", "evidence", "levels"],
      "properties": {
        "key": { "$ref": "#/$defs/rubricKey" },
        "title": { "type": "string", "minLength": 1, "maxLength": 2000 },
        "points": { "type": "number", "exclusiveMinimum": 0, "maximum": 1000 },
        "observable": { "type": "string", "minLength": 1, "maxLength": 2000 },
        "evidence": { "type": "string", "minLength": 1, "maxLength": 2000 },
        "levels": {
          "type": "array",
          "maxItems": 10,
          "items": { "$ref": "#/$defs/rubricLevel" }
        }
      }
    },
    "structuredRubric": {
      "type": "object",
      "additionalProperties": false,
      "required": ["version", "criteria"],
      "properties": {
        "version": { "const": 1 },
        "criteria": {
          "type": "array",
          "minItems": 1,
          "maxItems": 30,
          "items": { "$ref": "#/$defs/rubricCriterion" }
        }
      }
    },
    "legacyRubric": {
      "type": "object",
      "additionalProperties": false,
      "required": ["legacy_text"],
      "properties": {
        "legacy_text": { "type": "string", "maxLength": 100000 }
      }
    },
    "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", "exclusiveMinimum": 0, "maximum": 1000 },
        "stem": { "type": "string", "minLength": 1, "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, "maximum": 200 },
        "composition_rows": { "type": "integer", "minimum": 1, "maximum": 200 },
        "rubric": {
          "oneOf": [
            { "$ref": "#/$defs/structuredRubric" },
            { "$ref": "#/$defs/legacyRubric" }
          ]
        },
        "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"] }
        }
      ]
    }
  }
}
