{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://raw.githubusercontent.com/urtuba/psytools/main/schema/assessment-definition.schema.json",
  "title": "AssessmentDefinition",
  "description": "A psytools assessment: questions with an enumerated option scale, localized text, and optional declarative scoring. Lets non-JavaScript backends validate stored definitions.",
  "type": "object",
  "required": ["id", "title", "defaultLocale", "options", "questions"],
  "additionalProperties": false,
  "properties": {
    "id": { "type": "string", "minLength": 1 },
    "version": { "type": "string" },
    "categories": {
      "type": "array",
      "description": "What the assessment measures, e.g. [\"depression\"]. Bundled inventories use a small controlled vocabulary; custom definitions may use any strings.",
      "items": { "type": "string", "minLength": 1 }
    },
    "audience": {
      "type": "array",
      "description": "Age band(s) the instrument is written and validated for — who it is about, e.g. [\"adult\"]. Bundled inventories use a small controlled vocabulary (adult, adolescent, child); custom definitions may use any strings.",
      "items": { "type": "string", "minLength": 1 }
    },
    "respondent": {
      "type": "string",
      "minLength": 1,
      "description": "Whose report the answers are: \"self\" for the person the assessment is about, otherwise a proxy informant (\"parent\", \"teacher\", \"clinician\"). Not the administration mode — an interviewer-read self-report is still \"self\". Custom definitions may use any string.",
      "examples": ["self", "parent", "teacher", "clinician"]
    },
    "title": { "$ref": "#/$defs/localizedText" },
    "description": { "$ref": "#/$defs/localizedText" },
    "instructions": { "$ref": "#/$defs/localizedText" },
    "defaultLocale": { "type": "string", "minLength": 1 },
    "options": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/option" }
    },
    "questions": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/question" }
    },
    "scoring": {
      "oneOf": [
        { "$ref": "#/$defs/sumScoring" },
        { "$ref": "#/$defs/subscaleScoring" },
        { "$ref": "#/$defs/countScoring" }
      ]
    },
    "meta": { "type": "object" }
  },
  "$defs": {
    "localizedText": {
      "type": "object",
      "description": "Text keyed by BCP-47 language tag, e.g. { \"en\": \"Not at all\", \"tr\": \"Hiç\" }.",
      "minProperties": 1,
      "additionalProperties": { "type": "string" }
    },
    "option": {
      "type": "object",
      "required": ["value", "label"],
      "additionalProperties": false,
      "properties": {
        "value": { "type": "number" },
        "label": { "$ref": "#/$defs/localizedText" }
      }
    },
    "question": {
      "type": "object",
      "required": ["id", "text"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "text": { "$ref": "#/$defs/localizedText" },
        "options": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/option" }
        },
        "reverseScored": { "type": "boolean" },
        "optional": { "type": "boolean" }
      }
    },
    "scoreBand": {
      "type": "object",
      "required": ["id", "min", "max", "label"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "min": { "type": "number" },
        "max": { "type": "number" },
        "label": { "$ref": "#/$defs/localizedText" }
      }
    },
    "scoreFlag": {
      "type": "object",
      "required": ["id", "questionId", "minValue", "label"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "questionId": { "type": "string", "minLength": 1 },
        "minValue": { "type": "number" },
        "label": { "$ref": "#/$defs/localizedText" }
      }
    },
    "missingDataPolicy": {
      "type": "object",
      "required": ["strategy"],
      "additionalProperties": false,
      "properties": {
        "strategy": { "enum": ["ignore", "prorate", "require-complete"] },
        "minAnswered": { "type": "number" }
      }
    },
    "sumScoring": {
      "type": "object",
      "required": ["kind"],
      "additionalProperties": false,
      "properties": {
        "kind": { "const": "sum" },
        "multiplier": { "type": "number" },
        "bands": { "type": "array", "items": { "$ref": "#/$defs/scoreBand" } },
        "flags": { "type": "array", "items": { "$ref": "#/$defs/scoreFlag" } },
        "missing": { "$ref": "#/$defs/missingDataPolicy" }
      }
    },
    "subscale": {
      "type": "object",
      "required": ["id", "label", "questionIds"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "label": { "$ref": "#/$defs/localizedText" },
        "questionIds": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "minLength": 1 }
        },
        "multiplier": { "type": "number" },
        "bands": { "type": "array", "items": { "$ref": "#/$defs/scoreBand" } }
      }
    },
    "countScoringItem": {
      "type": "object",
      "required": ["questionId", "minValue"],
      "additionalProperties": false,
      "properties": {
        "questionId": { "type": "string", "minLength": 1 },
        "minValue": { "type": "number" }
      }
    },
    "countScoring": {
      "type": "object",
      "required": ["kind", "items"],
      "additionalProperties": false,
      "properties": {
        "kind": { "const": "count" },
        "items": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/countScoringItem" }
        },
        "bands": { "type": "array", "items": { "$ref": "#/$defs/scoreBand" } },
        "flags": { "type": "array", "items": { "$ref": "#/$defs/scoreFlag" } },
        "missing": { "$ref": "#/$defs/missingDataPolicy" }
      }
    },
    "subscaleScoring": {
      "type": "object",
      "required": ["kind", "subscales"],
      "additionalProperties": false,
      "properties": {
        "kind": { "const": "subscales" },
        "subscales": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/subscale" }
        },
        "flags": { "type": "array", "items": { "$ref": "#/$defs/scoreFlag" } },
        "missing": { "$ref": "#/$defs/missingDataPolicy" }
      }
    }
  }
}
