{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://mnstry.org/contracts/org/org-writing.v1.schema.json",
  "title": "MNSTRY.org writing v1",
  "description": "A public writing head with its topic memberships, immutable revision ledger, body, and research apparatus.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "contract",
    "slug",
    "kg",
    "title",
    "abstract",
    "kind",
    "topics",
    "courseMemberships",
    "version",
    "releaseHash",
    "guidelinesVersion",
    "versions",
    "canonicalPath",
    "body"
  ],
  "properties": {
    "schema": { "const": "org-writing@v1" },
    "contract": { "const": "https://mnstry.org/contracts/org/org-writing.v1.schema.json" },
    "slug": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" },
    "kg": { "$ref": "#/$defs/kg" },
    "title": { "type": "string", "minLength": 1 },
    "subtitle": { "type": "string", "minLength": 1 },
    "abstract": { "type": "string", "minLength": 1 },
    "kind": { "enum": ["essay", "note", "research", "brick"] },
    "topics": {
      "type": "array",
      "uniqueItems": true,
      "items": { "type": "string", "minLength": 1 }
    },
    "courseMemberships": {
      "type": "array",
      "items": { "$ref": "#/$defs/courseMembership" }
    },
    "publishedAt": { "type": "string", "format": "date-time" },
    "updatedAt": { "type": "string", "format": "date-time" },
    "version": { "type": "integer", "minimum": 1 },
    "releaseHash": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
    "guidelinesVersion": { "type": "integer", "minimum": 1 },
    "brief": { "$ref": "#/$defs/brief" },
    "sources": {
      "type": "array",
      "items": { "$ref": "#/$defs/source" }
    },
    "versions": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/version" }
    },
    "canonicalPath": { "type": "string", "pattern": "^/writing/[a-z0-9]+(?:-[a-z0-9]+)*/$" },
    "body": { "type": "string" },
    "apparatus": { "$ref": "#/$defs/apparatus" }
  },
  "$defs": {
    "kgId": {
      "type": "string",
      "pattern": "^org:[a-z0-9][a-z0-9:.-]*$"
    },
    "relations": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "related": { "$ref": "#/$defs/relationTargets" },
        "supports": { "$ref": "#/$defs/relationTargets" },
        "supersedes": { "$ref": "#/$defs/relationTargets" },
        "implements": { "$ref": "#/$defs/relationTargets" },
        "depends_on": { "$ref": "#/$defs/relationTargets" },
        "evidences": { "$ref": "#/$defs/relationTargets" },
        "contradicts": { "$ref": "#/$defs/relationTargets" },
        "belongs_to": { "$ref": "#/$defs/relationTargets" }
      }
    },
    "relationTargets": {
      "type": "array",
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/kgId" }
    },
    "kg": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "graph"],
      "properties": {
        "id": { "$ref": "#/$defs/kgId" },
        "type": { "enum": ["essay", "note", "research", "brick"] },
        "relations": { "$ref": "#/$defs/relations" },
        "graph": { "const": "/kg.json" }
      }
    },
    "courseMembership": {
      "type": "object",
      "additionalProperties": false,
      "required": ["course", "topic", "wall", "position", "total"],
      "properties": {
        "course": { "type": "string", "pattern": "^org:courses:[a-z0-9][a-z0-9.-]*$" },
        "topic": { "type": "string", "minLength": 1 },
        "wall": { "type": "string", "pattern": "^org:walls:[a-z0-9][a-z0-9.-]*$" },
        "position": { "type": "integer", "minimum": 1 },
        "total": { "type": "integer", "minimum": 2 }
      }
    },
    "briefCell": {
      "type": "object",
      "additionalProperties": false,
      "required": ["text", "claims"],
      "properties": {
        "text": { "type": "string", "minLength": 1 },
        "claims": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        }
      }
    },
    "brief": {
      "type": "object",
      "additionalProperties": false,
      "required": ["problem", "mechanism", "move"],
      "properties": {
        "problem": { "$ref": "#/$defs/briefCell" },
        "mechanism": { "$ref": "#/$defs/briefCell" },
        "move": { "$ref": "#/$defs/briefCell" }
      }
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["repo", "path"],
      "properties": {
        "repo": { "type": "string", "minLength": 1 },
        "path": { "type": "string", "minLength": 1 }
      }
    },
    "version": {
      "type": "object",
      "additionalProperties": false,
      "required": ["version", "cutAt", "note", "visibility", "path"],
      "properties": {
        "version": { "type": "integer", "minimum": 1 },
        "cutAt": { "type": "string", "format": "date" },
        "note": { "type": "string", "minLength": 1 },
        "visibility": { "enum": ["published", "retired"] },
        "path": {
          "anyOf": [
            { "type": "string", "pattern": "^/writing/[a-z0-9]+(?:-[a-z0-9]+)*(?:/v/[1-9][0-9]*)?/$" },
            { "type": "null" }
          ]
        },
        "contentHash": { "type": "string", "pattern": "^sha256:[a-f0-9]{16}$" },
        "releaseHash": { "type": "string", "pattern": "^[a-f0-9]{64}$" }
      }
    },
    "reference": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "author", "work", "relevance"],
      "properties": {
        "id": { "$ref": "#/$defs/kgId" },
        "author": { "type": "string", "minLength": 1 },
        "work": { "type": "string", "minLength": 1 },
        "year": { "type": "integer" },
        "relevance": { "type": "string", "minLength": 1 },
        "sourceType": { "enum": ["book", "paper", "report", "case", "dataset", "website", "interview", "other"] },
        "url": { "type": "string", "format": "uri" },
        "doi": { "type": "string", "minLength": 1 },
        "locator": { "type": "string", "minLength": 1 }
      }
    },
    "claim": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "claim", "basis", "confidence", "sources"],
      "properties": {
        "id": { "$ref": "#/$defs/kgId" },
        "claim": { "type": "string", "minLength": 1 },
        "basis": { "type": "string", "minLength": 1 },
        "confidence": { "enum": ["verified", "directional", "contested"] },
        "sources": {
          "type": "array",
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/kgId" }
        }
      }
    },
    "concept": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "definition"],
      "properties": {
        "id": { "$ref": "#/$defs/kgId" },
        "name": { "type": "string", "minLength": 1 },
        "definition": { "type": "string", "minLength": 1 },
        "provenance": { "enum": ["canonical", "local"] }
      }
    },
    "apparatus": {
      "type": "object",
      "additionalProperties": false,
      "required": ["note", "references", "claims", "concepts", "researchContext"],
      "properties": {
        "note": { "type": "string", "minLength": 1 },
        "references": {
          "type": "array",
          "items": { "$ref": "#/$defs/reference" }
        },
        "claims": {
          "type": "array",
          "items": { "$ref": "#/$defs/claim" }
        },
        "concepts": {
          "type": "array",
          "items": { "$ref": "#/$defs/concept" }
        },
        "researchContext": { "type": "string" }
      }
    }
  }
}
