{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://dicworkflowkit.local/schemas/ontology-snapshot.schema.json",
  "title": "DIC Ontology Snapshot",
  "description": "Portable, file-backed semantic graph for design-implementation consistency.",
  "type": "object",
  "required": [
    "schema",
    "version",
    "snapshotId",
    "projectRoot",
    "objects",
    "links",
    "invariantResults"
  ],
  "properties": {
    "schema": { "const": "dic.ontology-snapshot.v1" },
    "version": { "type": "string" },
    "snapshotId": { "$ref": "#/$defs/stableId" },
    "snapshotRevision": {
      "type": "string",
      "pattern": "^[a-f0-9]{64}$"
    },
    "projectRoot": { "type": "string" },
    "objects": {
      "type": "array",
      "items": { "$ref": "#/$defs/object" }
    },
    "links": {
      "type": "array",
      "items": { "$ref": "#/$defs/link" }
    },
    "invariantResults": {
      "type": "array",
      "items": { "$ref": "#/$defs/invariantResult" }
    },
    "appliedActionIds": {
      "type": "array",
      "items": {
        "type": "string",
        "pattern": "^act-[a-zA-Z0-9._:-]+$"
      },
      "uniqueItems": true
    }
  },
  "$defs": {
    "stableId": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9-]*-[a-zA-Z0-9._:-]+$"
    },
    "lifecycle": {
      "enum": [
        "draft",
        "confirmed",
        "active",
        "deprecated",
        "superseded",
        "rejected",
        "open",
        "approved",
        "in_progress",
        "resolved",
        "blocked"
      ]
    },
    "provenance": {
      "type": "object",
      "required": ["source", "sourceRevision", "createdBy"],
      "properties": {
        "source": { "type": "string", "minLength": 1 },
        "sourceLocator": { "type": "string" },
        "sourceRevision": { "type": "string", "minLength": 1 },
        "contentHash": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        },
        "createdBy": { "type": "string", "minLength": 1 },
        "runId": { "type": "string" }
      },
      "additionalProperties": false
    },
    "object": {
      "type": "object",
      "required": ["id", "type", "lifecycle", "properties", "provenance"],
      "properties": {
        "id": { "$ref": "#/$defs/stableId" },
        "type": {
          "enum": [
            "Project",
            "DesignSource",
            "Change",
            "Requirement",
            "Scenario",
            "ImplementationUnit",
            "Gap",
            "RepairAction",
            "Validation",
            "Evidence",
            "AgentRun",
            "Decision",
            "Risk",
            "Candidate",
            "GateRun",
            "Claim",
            "Challenge",
            "Counterexample",
            "Waiver",
            "Policy"
          ]
        },
        "lifecycle": { "$ref": "#/$defs/lifecycle" },
        "interfaces": {
          "type": "array",
          "items": {
            "enum": [
              "SourceBacked",
              "Verifiable",
              "Versioned",
              "GovernedMutation",
              "ContentAddressed"
            ]
          },
          "uniqueItems": true
        },
        "properties": {
          "type": "object",
          "additionalProperties": true
        },
        "provenance": { "$ref": "#/$defs/provenance" }
      },
      "allOf": [
        {
          "if": {
            "properties": { "type": { "const": "Evidence" } },
            "required": ["type"]
          },
          "then": {
            "properties": {
              "interfaces": {
                "contains": { "const": "ContentAddressed" }
              },
              "properties": {
                "required": ["path"],
                "properties": {
                  "path": { "type": "string", "minLength": 1 }
                }
              },
              "provenance": {
                "required": ["contentHash"]
              }
            }
          }
        },
        {
          "if": {
            "properties": { "type": { "const": "ImplementationUnit" } },
            "required": ["type"]
          },
          "then": {
            "properties": {
              "interfaces": {
                "contains": { "const": "ContentAddressed" }
              },
              "properties": {
                "required": ["path"],
                "properties": {
                  "path": { "type": "string", "minLength": 1 }
                }
              },
              "provenance": {
                "required": ["contentHash"]
              }
            }
          }
        },
        {
          "if": {
            "properties": { "type": { "const": "Validation" } },
            "required": ["type"]
          },
          "then": {
            "properties": {
              "properties": {
                "required": ["status", "executedAt", "inputs"],
                "properties": {
                  "status": { "enum": ["PASS", "FAIL", "BLOCKED"] },
                  "executedAt": { "type": "string", "format": "date-time" },
                  "inputs": {
                    "type": "array",
                    "items": { "$ref": "#/$defs/validationInput" }
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": { "type": { "const": "Candidate" } },
            "required": ["type"]
          },
          "then": {
            "properties": {
              "interfaces": {
                "contains": { "const": "ContentAddressed" }
              },
              "properties": {
                "required": ["ref"],
                "properties": {
                  "ref": { "type": "string", "minLength": 1 }
                }
              },
              "provenance": {
                "required": ["contentHash"]
              }
            }
          }
        },
        {
          "if": {
            "properties": { "type": { "const": "GateRun" } },
            "required": ["type"]
          },
          "then": {
            "properties": {
              "properties": {
                "required": ["status", "executedAt"],
                "properties": {
                  "status": { "enum": ["PASS", "FAIL", "BLOCKED", "SKIPPED"] },
                  "executedAt": { "type": "string", "format": "date-time" }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "enum": ["Claim", "Challenge", "Counterexample", "Waiver", "Policy"]
              }
            },
            "required": ["type"]
          },
          "then": {
            "properties": {
              "properties": {
                "required": ["statement"],
                "properties": {
                  "statement": { "type": "string", "minLength": 1 }
                }
              }
            }
          }
        }
      ],
      "additionalProperties": false
    },
    "validationInput": {
      "type": "object",
      "required": ["objectId", "contentHash"],
      "properties": {
        "objectId": { "$ref": "#/$defs/stableId" },
        "contentHash": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        }
      },
      "additionalProperties": false
    },
    "link": {
      "type": "object",
      "required": ["id", "type", "from", "to", "lifecycle", "provenance"],
      "properties": {
        "id": { "$ref": "#/$defs/stableId" },
        "type": {
          "enum": [
            "CONTAINS",
            "PROPOSES",
            "DERIVED_FROM",
            "REFINES",
            "IMPLEMENTED_BY",
            "VERIFIED_BY",
            "VIOLATES",
            "RESOLVES",
            "PRODUCES",
            "SUPPORTS",
            "CONTRADICTS",
            "REMAINS_AFTER",
            "EVALUATES",
            "DECIDES",
            "ASSERTS",
            "CHALLENGES",
            "REFUTES",
            "SUPPORTED_BY",
            "WAIVES",
            "GOVERNS",
            "SUPERSEDES"
          ]
        },
        "from": { "$ref": "#/$defs/stableId" },
        "to": { "$ref": "#/$defs/stableId" },
        "lifecycle": { "$ref": "#/$defs/lifecycle" },
        "provenance": { "$ref": "#/$defs/provenance" }
      },
      "additionalProperties": false
    },
    "invariantResult": {
      "type": "object",
      "required": ["id", "status", "severity", "message", "objectIds"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "status": { "enum": ["PASS", "FAIL", "NOT_APPLICABLE"] },
        "severity": { "enum": ["info", "warning", "blocking"] },
        "message": { "type": "string" },
        "objectIds": {
          "type": "array",
          "items": { "$ref": "#/$defs/stableId" },
          "uniqueItems": true
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
