{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:junexis:schema:installation-journal:0.1",
  "title": "Dziennik operacji instalacyjnej Junexis 0.1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "operationId", "operation", "rid", "planSha256", "startedAt", "completedAt", "result", "events"],
  "properties": {
    "schemaVersion": { "const": "junexis.installation-journal/0.1" },
    "operationId": { "$ref": "#/$defs/identifier" },
    "operation": { "enum": ["install", "update", "rollback", "repair", "uninstall", "cleanup"] },
    "rid": { "enum": ["win-x64", "linux-x64", "osx-arm64"] },
    "planSha256": { "$ref": "#/$defs/sha256" },
    "startedAt": { "$ref": "#/$defs/timestamp" },
    "completedAt": { "oneOf": [{ "$ref": "#/$defs/timestamp" }, { "type": "null" }] },
    "result": { "enum": ["in-progress", "committed", "rolled-back", "cancelled", "failed", "recovery-required"] },
    "events": {
      "type": "array",
      "minItems": 1,
      "maxItems": 4096,
      "items": { "$ref": "#/$defs/event" }
    }
  },
  "allOf": [
    {
      "if": { "properties": { "result": { "const": "in-progress" } }, "required": ["result"] },
      "then": { "properties": { "completedAt": { "type": "null" } } }
    },
    {
      "if": { "not": { "properties": { "result": { "const": "in-progress" } }, "required": ["result"] } },
      "then": { "properties": { "completedAt": { "$ref": "#/$defs/timestamp" } } }
    }
  ],
  "$defs": {
    "identifier": { "type": "string", "pattern": "^[a-z0-9][a-z0-9._-]{1,127}$" },
    "sha256": { "type": "string", "pattern": "^[0-9a-f]{64}(?![\\s\\S])" },
    "timestamp": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$" },
    "event": {
      "type": "object",
      "additionalProperties": false,
      "required": ["sequence", "phase", "occurredAt", "code", "message"],
      "properties": {
        "sequence": { "type": "integer", "minimum": 1 },
        "phase": { "enum": ["planned", "staging", "staged", "verifying", "verified", "installing", "installed-inactive", "activating", "health-checking", "committing", "committed", "rolling-back", "rolled-back", "cancelled", "failed", "recovery-required"] },
        "occurredAt": { "$ref": "#/$defs/timestamp" },
        "code": { "$ref": "#/$defs/identifier" },
        "message": { "type": "string", "minLength": 1, "maxLength": 512 }
      }
    }
  }
}
