{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/rhys-looma/usher/spec/plan.schema.json",
  "title": "Usher plan",
  "type": "object",
  "required": ["usher_plan", "origin", "intent", "start", "steps", "inputs", "consequential", "complete", "stops_at"],
  "additionalProperties": false,
  "properties": {
    "usher_plan": { "const": "0.1" },
    "origin": { "type": "string", "pattern": "^https?://[^/]+$" },
    "intent": { "type": ["string", "null"] },
    "start": { "type": "string", "pattern": "^/" },
    "steps": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": ["path", "map", "purpose", "advance", "leads_to"],
        "additionalProperties": false,
        "properties": {
          "path": { "type": "string", "pattern": "^/" },
          "map": { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" },
          "purpose": { "type": "string" },
          "advance": {
            "anyOf": [
              {
                "type": "object",
                "required": ["id", "key", "label", "risk"],
                "additionalProperties": false,
                "properties": { "id": { "type": "string" }, "key": { "$ref": "#/$defs/key" }, "label": { "type": "string" }, "risk": { "$ref": "#/$defs/risk" } }
              },
              { "type": "null" }
            ]
          },
          "leads_to": { "type": ["string", "null"] }
        }
      }
    },
    "inputs": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["key", "label", "control", "required", "risk", "steps"],
        "additionalProperties": false,
        "properties": {
          "key": { "$ref": "#/$defs/key" },
          "label": { "type": "string" },
          "control": { "type": "string" },
          "required": { "type": "boolean" },
          "risk": { "$ref": "#/$defs/risk" },
          "sensitive": { "type": "boolean" },
          "constraints": { "type": "object" },
          "options": {
            "type": "object",
            "required": ["items", "total", "truncated"],
            "properties": {
              "items": { "type": "array", "items": { "type": "object", "required": ["value", "label"], "properties": { "value": { "type": "string" }, "label": { "type": "string" } } } },
              "total": { "type": "integer" },
              "truncated": { "type": "boolean" }
            }
          },
          "shown_if": { "type": "object" },
          "steps": { "type": "array", "minItems": 1, "items": { "type": "string" } }
        }
      }
    },
    "consequential": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["path", "key", "label", "risk"],
        "additionalProperties": false,
        "properties": { "path": { "type": "string" }, "key": { "$ref": "#/$defs/key" }, "label": { "type": "string" }, "risk": { "$ref": "#/$defs/risk" } }
      }
    },
    "complete": { "type": "boolean" },
    "stops_at": { "type": ["string", "null"] }
  },
  "$defs": {
    "key": { "type": "string", "pattern": "^[a-z][a-z0-9_]*(\\.[a-z][a-z0-9_]*)+$" },
    "risk": { "enum": ["safe", "consequential", "needs_user"] }
  }
}
