{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://mnstry.org/contracts/org/org-knowledge-graph.v1.schema.json",
  "title": "MNSTRY.org knowledge graph v1",
  "description": "The complete public MNSTRY.org graph. Layers identify semantic meaning, reader navigation, and provenance inside one graph contract.",
  "type": "object",
  "additionalProperties": false,
  "required": ["schema", "contract", "ontologyVersion", "generatedAt", "sourceHash", "nodes", "edges"],
  "properties": {
    "schema": { "const": "org-knowledge-graph@v1" },
    "contract": { "const": "https://mnstry.org/contracts/org/org-knowledge-graph.v1.schema.json" },
    "ontologyVersion": { "type": "integer", "minimum": 1 },
    "generatedAt": { "type": "string", "format": "date-time" },
    "sourceHash": {
      "description": "SHA-256 binding to graph-defining repository sources. It is provenance, not graph meaning.",
      "type": "string",
      "pattern": "^[a-f0-9]{64}$"
    },
    "nodes": {
      "type": "array",
      "items": { "$ref": "#/$defs/node" }
    },
    "edges": {
      "type": "array",
      "items": { "$ref": "#/$defs/edge" }
    }
  },
  "$defs": {
    "kgId": {
      "type": "string",
      "pattern": "^org:[a-z0-9][a-z0-9:.-]*$"
    },
    "absoluteUrl": {
      "type": "string",
      "format": "uri",
      "pattern": "^https://"
    },
    "layer": {
      "enum": ["semantic", "navigation", "provenance"]
    },
    "junction": {
      "type": "object",
      "additionalProperties": false,
      "required": ["brick", "transition", "targetCourse"],
      "properties": {
        "brick": { "$ref": "#/$defs/kgId" },
        "transition": { "type": "string", "minLength": 1 },
        "targetCourse": { "$ref": "#/$defs/kgId" }
      }
    },
    "step": {
      "type": "object",
      "additionalProperties": false,
      "required": ["brick", "transition"],
      "properties": {
        "brick": { "$ref": "#/$defs/kgId" },
        "transition": { "type": "string", "minLength": 1 },
        "junction": { "$ref": "#/$defs/junction" }
      }
    },
    "node": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "layers"],
      "properties": {
        "id": { "$ref": "#/$defs/kgId" },
        "type": {
          "enum": ["essay", "note", "research", "brick", "apparatus", "concept", "pillar", "course", "wall", "surface"]
        },
        "layers": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/layer" }
        },
        "title": { "type": "string", "minLength": 1 },
        "subtitle": { "type": "string", "minLength": 1 },
        "abstract": { "type": "string", "minLength": 1 },
        "name": { "type": "string", "minLength": 1 },
        "aliases": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string", "minLength": 1 }
        },
        "form": { "enum": ["term", "framework", "model", "test"] },
        "conceptStatus": { "enum": ["canonical", "deprecated"] },
        "definition": { "type": "string", "minLength": 1 },
        "introducedIn": { "$ref": "#/$defs/kgId" },
        "appearsIn": {
          "type": "array",
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/kgId" }
        },
        "deprecated": { "const": true },
        "supersededBy": { "$ref": "#/$defs/kgId" },
        "code": { "type": "string", "minLength": 1 },
        "description": { "type": "string", "minLength": 1 },
        "topic": { "type": "string", "minLength": 1 },
        "practice": { "type": "string", "minLength": 1 },
        "framing": { "type": "string", "minLength": 1 },
        "steps": {
          "type": "array",
          "minItems": 2,
          "items": { "$ref": "#/$defs/step" }
        },
        "completion": { "type": "string", "minLength": 1 },
        "courses": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/kgId" }
        },
        "order": { "type": "integer" },
        "evidences": { "$ref": "#/$defs/kgId" },
        "url": { "$ref": "#/$defs/absoluteUrl" },
        "json": { "$ref": "#/$defs/absoluteUrl" },
        "version": { "type": "integer", "minimum": 1 },
        "publishedAt": { "type": "string", "format": "date-time" },
        "updatedAt": { "type": "string", "format": "date-time" },
        "formerIds": {
          "type": "array",
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/kgId" }
        },
        "contentHash": { "type": "string", "pattern": "^sha256:[a-f0-9]{16}$" },
        "releaseHash": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "cutAt": { "type": "string", "format": "date-time" }
      },
      "allOf": [
        {
          "if": { "properties": { "type": { "const": "course" } }, "required": ["type"] },
          "then": { "required": ["name", "topic", "practice", "framing", "steps", "completion", "url", "json"] }
        },
        {
          "if": { "properties": { "type": { "const": "wall" } }, "required": ["type"] },
          "then": { "required": ["name", "order", "courses", "framing"] }
        },
        {
          "if": { "properties": { "type": { "const": "concept" } }, "required": ["type"] },
          "then": { "required": ["name", "aliases", "form", "conceptStatus", "definition", "introducedIn", "url"] }
        },
        {
          "if": {
            "properties": { "type": { "const": "concept" }, "conceptStatus": { "const": "deprecated" } },
            "required": ["type", "conceptStatus"]
          },
          "then": { "required": ["deprecated", "supersededBy"] }
        }
      ]
    },
    "edge": {
      "type": "object",
      "additionalProperties": false,
      "required": ["subject", "predicate", "object", "provenance", "layer"],
      "properties": {
        "subject": { "$ref": "#/$defs/kgId" },
        "predicate": {
          "enum": ["related", "supports", "supersedes", "implements", "depends_on", "evidences", "contradicts", "belongs_to", "elaborates", "sequences"]
        },
        "object": { "$ref": "#/$defs/kgId" },
        "provenance": { "enum": ["structural", "editorial"] },
        "layer": { "$ref": "#/$defs/layer" }
      }
    }
  }
}
