{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Science Simulation SimSpec v1",
  "description": "Schema for self-contained STEM simulations generated by the science-sim-author skill.",
  "type": "object",
  "required": [
    "id",
    "title",
    "domain",
    "state",
    "params",
    "initial",
    "equations",
    "outputs"
  ],
  "properties": {
    "id": {
      "type": "string",
      "minLength": 1,
      "pattern": "^[a-z0-9]+(?:[_-][a-z0-9]+)*$"
    },
    "title": {
      "type": "string",
      "minLength": 1
    },
    "domain": {
      "type": "string",
      "enum": [
        "mechanics",
        "electromagnetism"
      ]
    },
    "level": {
      "type": "string",
      "minLength": 1
    },
    "dt": {
      "type": "object",
      "required": [
        "default",
        "min",
        "max"
      ],
      "properties": {
        "default": {
          "type": "number",
          "exclusiveMinimum": 0
        },
        "min": {
          "type": "number",
          "exclusiveMinimum": 0
        },
        "max": {
          "type": "number",
          "exclusiveMinimum": 0
        }
      },
      "additionalProperties": false
    },
    "state": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/definitions/stateVar"
      }
    },
    "params": {
      "type": "array",
      "minItems": 2,
      "maxItems": 6,
      "items": {
        "$ref": "#/definitions/param"
      }
    },
    "initial": {
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
      },
      "additionalProperties": {
        "type": "number"
      }
    },
    "equations": {
      "type": "object",
      "minProperties": 1,
      "patternProperties": {
        "^d[A-Za-z_][A-Za-z0-9_]*$": {
          "type": "string",
          "minLength": 1
        },
        "^(dx|dy|dz|dvx|dvy|dvz|dq|dvc|di|dv)$": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": false
    },
    "outputs": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/definitions/output"
      }
    },
    "worksheet": {
      "$ref": "#/definitions/worksheet"
    },
    "success_criteria": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "string",
        "minLength": 1
      }
    }
  },
  "additionalProperties": {
    "$ref": "#/definitions/extensionValue"
  },
  "definitions": {
    "stateVar": {
      "type": "object",
      "required": [
        "name"
      ],
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
        },
        "unit": {
          "type": "string",
          "minLength": 1
        },
        "desc": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": false
    },
    "param": {
      "type": "object",
      "required": [
        "name",
        "value",
        "min",
        "max"
      ],
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
        },
        "unit": {
          "type": "string",
          "minLength": 1
        },
        "value": {
          "type": "number"
        },
        "min": {
          "type": "number"
        },
        "max": {
          "type": "number"
        },
        "step": {
          "type": "number",
          "exclusiveMinimum": 0
        },
        "desc": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": false
    },
    "traceOutput": {
      "type": "object",
      "required": [
        "type",
        "what"
      ],
      "properties": {
        "type": {
          "const": "trace"
        },
        "what": {
          "type": "array",
          "minItems": 2,
          "maxItems": 2,
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "label": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": false
    },
    "plotOutput": {
      "type": "object",
      "required": [
        "type",
        "x",
        "y"
      ],
      "properties": {
        "type": {
          "const": "plot"
        },
        "x": {
          "type": "string",
          "minLength": 1
        },
        "y": {
          "type": "string",
          "minLength": 1
        },
        "label": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": false
    },
    "output": {
      "oneOf": [
        {
          "$ref": "#/definitions/traceOutput"
        },
        {
          "$ref": "#/definitions/plotOutput"
        }
      ]
    },
    "promptList": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "string",
        "minLength": 1
      }
    },
    "worksheet": {
      "type": "object",
      "properties": {
        "predict": {
          "$ref": "#/definitions/promptList"
        },
        "test": {
          "$ref": "#/definitions/promptList"
        },
        "explain": {
          "$ref": "#/definitions/promptList"
        },
        "misconceptions": {
          "$ref": "#/definitions/promptList"
        }
      },
      "additionalProperties": false
    },
    "extensionValue": {
      "description": "Optional metadata-only extensions. Keep these deterministic and non-executable.",
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "number"
        },
        {
          "type": "boolean"
        },
        {
          "type": "array",
          "items": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "number"
              },
              {
                "type": "boolean"
              },
              {
                "type": "object"
              }
            ]
          }
        },
        {
          "type": "object"
        }
      ]
    }
  }
}
