{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://canon.eventinn.net/_schemas/canon-record.schema.json",
  "title": "EventInn Canon Record Metadata Schema",
  "description": "Machine-verifiable governance gate for every Canon record (CAN-GOV-META-001). accepted != effective; four status axes; controlled extension.",
  "type": "object",
  "required": [
    "canon_id",
    "title",
    "canon_area",
    "record_type",
    "record_plane",
    "governance_status",
    "decision_level",
    "risk_class",
    "owner",
    "schema_version",
    "created_at",
    "updated_at"
  ],
  "additionalProperties": false,
  "properties": {
    "canon_id": {
      "type": "string",
      "pattern": "^CAN-(CON|GOV|STR|REQ|DEC|ENG|AIO|ASR|TRC|RET)-[A-Z]{2,8}-[0-9]{3,4}$"
    },
    "title": {
      "type": "string",
      "minLength": 3
    },
    "canon_area": {
      "type": "string",
      "enum": [
        "constitution",
        "governance",
        "strategy",
        "requirements",
        "decisions",
        "engineering",
        "ai-operations",
        "assurance",
        "traceability",
        "retired"
      ]
    },
    "record_type": {
      "type": "string",
      "enum": [
        "charter",
        "scope",
        "authority",
        "policy",
        "standard",
        "schema",
        "strategy",
        "capability",
        "use_case",
        "business_rule",
        "invariant",
        "adr",
        "control",
        "slo",
        "runbook",
        "brief",
        "evidence",
        "standards_profile",
        "registry",
        "template",
        "index"
      ]
    },
    "record_plane": {
      "type": "string",
      "enum": [
        "normative",
        "execution",
        "evidence",
        "learning"
      ]
    },
    "governance_status": {
      "type": "string",
      "enum": [
        "draft",
        "proposed",
        "accepted",
        "effective",
        "superseded",
        "retired",
        "withdrawn",
        "rejected",
        "suspended",
        "conflicted"
      ]
    },
    "implementation_status": {
      "type": "string",
      "enum": [
        "not_applicable",
        "not_started",
        "partial",
        "implemented"
      ]
    },
    "evidence_status": {
      "type": "string",
      "enum": [
        "none",
        "reported",
        "observed",
        "reproducible",
        "independently_verified"
      ]
    },
    "deployment_status": {
      "type": "string",
      "enum": [
        "not_applicable",
        "not_deployed",
        "staging",
        "production"
      ]
    },
    "decision_level": {
      "type": "string",
      "enum": [
        "constitutional",
        "strategic",
        "tactical",
        "operational"
      ]
    },
    "risk_class": {
      "type": "string",
      "enum": [
        "R0",
        "R1",
        "R2",
        "R3",
        "R4"
      ]
    },
    "owner": {
      "type": "string"
    },
    "approved_by": {
      "type": [
        "string",
        "null"
      ]
    },
    "approved_at": {
      "anyOf": [
        {
          "type": "null"
        },
        {
          "type": "string",
          "format": "date-time",
          "pattern": "^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])T[0-9]{2}:[0-9]{2}:[0-9]{2}([.][0-9]+)?(Z|[+-][0-9]{2}:[0-9]{2})$"
        }
      ]
    },
    "effective_at": {
      "anyOf": [
        {
          "type": "null"
        },
        {
          "type": "string",
          "format": "date-time",
          "pattern": "^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])T[0-9]{2}:[0-9]{2}:[0-9]{2}([.][0-9]+)?(Z|[+-][0-9]{2}:[0-9]{2})$"
        }
      ]
    },
    "supersedes": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "superseded_by": {
      "type": [
        "string",
        "null"
      ]
    },
    "related": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "traceability": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "upstream": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "downstream": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "provenance": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "evidence_refs": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "schema_version": {
      "type": "integer",
      "minimum": 1
    },
    "created_at": {
      "type": "string",
      "format": "date",
      "pattern": "^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$"
    },
    "updated_at": {
      "type": "string",
      "format": "date",
      "pattern": "^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$"
    },
    "extensions": {
      "type": "object"
    },
    "slug": {
      "type": "string"
    },
    "sidebar_position": {
      "type": "number"
    },
    "sidebar_label": {
      "type": "string"
    },
    "sidebar_class_name": {
      "type": "string"
    },
    "hide_table_of_contents": {
      "type": "boolean"
    },
    "draft": {
      "type": "boolean"
    },
    "description": {
      "type": "string"
    },
    "keywords": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "tags": {
      "type": "array"
    },
    "pagination_next": {
      "type": [
        "string",
        "null"
      ]
    },
    "pagination_prev": {
      "type": [
        "string",
        "null"
      ]
    }
  },
  "allOf": [
    {
      "$comment": "evidence-plane => evidence_status",
      "if": {
        "properties": {
          "record_plane": {
            "const": "evidence"
          }
        },
        "required": [
          "record_plane"
        ]
      },
      "then": {
        "required": [
          "evidence_status"
        ]
      }
    },
    {
      "$comment": "accepted => approver + approved_at (non-null)",
      "if": {
        "properties": {
          "governance_status": {
            "const": "accepted"
          }
        },
        "required": [
          "governance_status"
        ]
      },
      "then": {
        "required": [
          "approved_by",
          "approved_at"
        ],
        "properties": {
          "approved_by": {
            "type": "string",
            "minLength": 1
          },
          "approved_at": {
            "type": "string",
            "minLength": 1
          }
        }
      }
    },
    {
      "$comment": "effective => approver + approved_at + effective_at (non-null)",
      "if": {
        "properties": {
          "governance_status": {
            "const": "effective"
          }
        },
        "required": [
          "governance_status"
        ]
      },
      "then": {
        "required": [
          "approved_by",
          "approved_at",
          "effective_at"
        ],
        "properties": {
          "approved_by": {
            "type": "string",
            "minLength": 1
          },
          "approved_at": {
            "type": "string",
            "minLength": 1
          },
          "effective_at": {
            "type": "string",
            "minLength": 1
          }
        }
      }
    },
    {
      "$comment": "staging => evidence_status >= observed",
      "if": {
        "properties": {
          "deployment_status": {
            "const": "staging"
          }
        },
        "required": [
          "deployment_status"
        ]
      },
      "then": {
        "required": [
          "evidence_status"
        ],
        "properties": {
          "evidence_status": {
            "enum": [
              "observed",
              "reproducible",
              "independently_verified"
            ]
          }
        }
      }
    },
    {
      "$comment": "production => independently_verified + evidence_refs",
      "if": {
        "properties": {
          "deployment_status": {
            "const": "production"
          }
        },
        "required": [
          "deployment_status"
        ]
      },
      "then": {
        "required": [
          "evidence_status",
          "evidence_refs"
        ],
        "properties": {
          "evidence_status": {
            "const": "independently_verified"
          },
          "evidence_refs": {
            "type": "array",
            "minItems": 1
          }
        }
      }
    },
    {
      "$comment": "effective_date forbidden",
      "not": {
        "required": [
          "effective_date"
        ]
      }
    }
  ]
}
