{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://course-quiz-lab.github.io/json-schema/v1/bank.schema.json",

  "$defs": {
    "metadata": {
      "title": "元数据",
      "description": "题库的元数据对象，将显示在刷题小站的题库内",
      "type": "object",
      "required": ["course", "author", "name"],
      "properties": {
        "name": {
          "title": "题库名称",
          "description": "题库的名称。格式：字符串"
        },
        "course": {
          "$ref": "#/$defs/course",
          "title": "课程全称",
          "description": "题目所属的课程全称。格式：字符串"
        },
        "author": {
          "type": "string",
          "minLength": 1,
          "title": "作者",
          "description": "题库的作者姓名。格式：字符串"
        },
        "source": {
          "type": "string",
          "minLength": 1,
          "title": "来源",
          "description": "题库的来源渠道，可以是题库的发布平台或是被转换文件的文件名。格式：字符串"
        },
        "sourceUrl": {
          "type": "string",
          "format": "uri",
          "title": "源文件链接",
          "description": "题库源文件的链接地址。格式：URI"
        }
      },
      "additionalProperties": false
    },
    "course": {
      "title": "课程名称",
      "description": "题目所属的课程名称。格式：字符串",
      "type": "string",
      "minLength": 1
    },
    "question": {
      "title": "题目",
      "description": "题目的通用结构，通过 type 字段区分具体题型",
      "allOf": [
        {
          "type": "object",
          "required": ["type"],
          "properties": {
            "type": {
              "type": "string",
              "enum": ["single", "multiple", "judge", "indeterminate"],
              "title": "题目类型",
              "description": "题目类型标识。可选值：single（单选）、multiple（多选）、judge（判断）、indeterminate（不定项）"
            }
          }
        },
        {
          "oneOf": [
            { "$ref": "#/$defs/singleChoice" },
            { "$ref": "#/$defs/multipleChoice" },
            { "$ref": "#/$defs/indeterminateChoice" },
            { "$ref": "#/$defs/judgeQuestion" }
          ]
        }
      ]
    },
    "questionBase": {
      "title": "题目基础字段",
      "description": "所有题型共有的基础字段",
      "type": "object",
      "required": ["type", "stem"],
      "properties": {
        "type": {
          "type": "string",
          "title": "题目类型",
          "description": "题目类型标识，与具体题型的 type 字段对应"
        },
        "stem": {
          "type": "string",
          "minLength": 1,
          "title": "题干",
          "description": "题目的题干内容。格式：字符串"
        },
        "analysis": {
          "type": "string",
          "title": "解析",
          "description": "题目的答案解析。格式：字符串"
        },
        "difficulty": {
          "type": "string",
          "title": "难度",
          "description": "题目的难度等级。格式：字符串"
        }
      }
    },
    "options": {
      "title": "选项",
      "description": "选择题的选项列表，至少包含两个选项。格式：字符串数组",
      "type": "array",
      "minItems": 2,
      "items": {
        "type": "string",
        "minLength": 1,
        "title": "选项内容",
        "description": "单个选项的文本内容。格式：字符串"
      }
    },
    "singleChoice": {
      "title": "单项选择题",
      "description": "单项选择题，从多个选项中选出一个正确答案",
      "allOf": [
        { "$ref": "#/$defs/questionBase" },
        {
          "type": "object",
          "required": ["type", "options", "answer"],
          "properties": {
            "type": {
              "const": "single",
              "title": "题目类型",
              "description": "固定值 single，表示单项选择题"
            },
            "options": {
              "$ref": "#/$defs/options",
              "title": "选项",
              "description": "可供选择的选项列表"
            },
            "answer": {
              "type": "integer",
              "minimum": 0,
              "title": "答案",
              "description": "正确选项的索引（从0开始）。格式：整数"
            }
          }
        }
      ],
      "unevaluatedProperties": false
    },
    "multipleChoice": {
      "title": "多项选择题",
      "description": "多项选择题，从多个选项中选出多个正确答案",
      "allOf": [
        { "$ref": "#/$defs/questionBase" },
        {
          "type": "object",
          "required": ["type", "options", "answer"],
          "properties": {
            "type": {
              "const": "multiple",
              "title": "题目类型",
              "description": "固定值 multiple，表示多项选择题"
            },
            "options": {
              "$ref": "#/$defs/options",
              "title": "选项",
              "description": "可供选择的选项列表"
            },
            "answer": {
              "type": "array",
              "minItems": 1,
              "uniqueItems": true,
              "items": {
                "type": "integer",
                "minimum": 0,
                "title": "答案选项",
                "description": "正确选项的索引（从0开始）。格式：整数"
              },
              "title": "答案",
              "description": "所有正确选项的索引列表。格式：整数数组"
            }
          }
        }
      ],
      "unevaluatedProperties": false
    },
    "indeterminateChoice": {
      "title": "不定向选择题",
      "description": "不定项选择题，答案数量不固定（可能为一个或多个）",
      "allOf": [
        { "$ref": "#/$defs/questionBase" },
        {
          "type": "object",
          "required": ["type", "options", "answer"],
          "properties": {
            "type": {
              "const": "indeterminate",
              "title": "题目类型",
              "description": "固定值 indeterminate，表示不定项选择题"
            },
            "options": {
              "$ref": "#/$defs/options",
              "title": "选项",
              "description": "可供选择的选项列表"
            },
            "answer": {
              "type": "array",
              "minItems": 1,
              "uniqueItems": true,
              "items": {
                "type": "integer",
                "minimum": 0,
                "title": "答案选项",
                "description": "正确选项的索引（从0开始）。格式：整数"
              },
              "title": "答案",
              "description": "正确选项的索引列表。格式：整数数组"
            }
          }
        }
      ],
      "unevaluatedProperties": false
    },
    "judgeQuestion": {
      "title": "判断题",
      "description": "判断题，判断给定的陈述是否正确",
      "allOf": [
        { "$ref": "#/$defs/questionBase" },
        {
          "type": "object",
          "required": ["type", "answer"],
          "properties": {
            "type": {
              "const": "judge",
              "title": "题目类型",
              "description": "固定值 judge，表示判断题"
            },
            "answer": {
              "type": "boolean",
              "title": "答案",
              "description": "判断题的答案，true 表示正确，false 表示错误。格式：布尔值"
            }
          }
        }
      ],
      "unevaluatedProperties": false
    }
  },
  "title": "题库",
  "description": "题库的根对象，包含元数据和题目列表",
  "type": "object",
  "required": ["metadata", "questions"],
  "properties": {
    "metadata": {
      "$ref": "#/$defs/metadata",
      "title": "元数据",
      "description": "题库的元数据对象，将显示在刷题小站的题库内"
    },
    "questions": {
      "title": "题目列表",
      "description": "题目数组，每一项为一个题目对象",
      "type": "array",
      "items": { "$ref": "#/$defs/question" }
    },
    "$schema": {
      "type": "string",
      "const": "https://course-quiz-lab.github.io/json-schema/v1/bank.schema.json",
      "title": "JSON Schema"
    }
  },
  "additionalProperties": false
}
