{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:junexis:schema:repair-plan:0.1",
  "title": "Plan naprawy instalacji Junexis 0.1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "planId", "operation", "createdAt", "rid", "expectedGeneration", "activeRelease", "source", "result", "blockers", "findings", "downloadBytes"],
  "properties": {
    "schemaVersion": { "const": "junexis.repair-plan/0.1" },
    "planId": { "$ref": "#/$defs/identifier" },
    "operation": { "const": "repair" },
    "createdAt": { "$ref": "#/$defs/timestamp" },
    "rid": { "enum": ["win-x64", "linux-x64", "osx-arm64"] },
    "expectedGeneration": { "type": "integer", "minimum": 1 },
    "activeRelease": { "$ref": "#/$defs/release" },
    "source": { "enum": ["verified-cache", "signed-catalog"] },
    "result": { "enum": ["ready", "no-change", "blocked"] },
    "blockers": { "type": "array", "maxItems": 128, "items": { "$ref": "#/$defs/blocker" } },
    "findings": { "type": "array", "maxItems": 4096, "items": { "$ref": "#/$defs/finding" } },
    "downloadBytes": { "type": "integer", "minimum": 0 }
  },
  "allOf": [
    {
      "if": { "properties": { "result": { "const": "ready" } }, "required": ["result"] },
      "then": { "properties": { "blockers": { "maxItems": 0 }, "findings": { "minItems": 1 } } }
    },
    {
      "if": { "properties": { "result": { "const": "no-change" } }, "required": ["result"] },
      "then": { "properties": { "blockers": { "maxItems": 0 }, "findings": { "maxItems": 0 }, "downloadBytes": { "const": 0 } } }
    },
    {
      "if": { "properties": { "result": { "const": "blocked" } }, "required": ["result"] },
      "then": { "properties": { "blockers": { "minItems": 1 } } }
    }
  ],
  "$defs": {
    "identifier": { "type": "string", "pattern": "^[a-z0-9][a-z0-9._-]{1,127}$" },
    "semver": { "type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+(?:[-+][0-9A-Za-z.-]+)?$" },
    "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$" },
    "release": {
      "type": "object",
      "additionalProperties": false,
      "required": ["releaseId", "version", "manifestSha256"],
      "properties": {
        "releaseId": { "$ref": "#/$defs/identifier" },
        "version": { "$ref": "#/$defs/semver" },
        "manifestSha256": { "$ref": "#/$defs/sha256" }
      }
    },
    "blocker": {
      "type": "object",
      "additionalProperties": false,
      "required": ["code", "message"],
      "properties": {
        "code": { "$ref": "#/$defs/identifier" },
        "message": { "type": "string", "minLength": 1, "maxLength": 512 }
      }
    },
    "finding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["component", "relativePath", "kind", "expectedSha256", "actualSha256", "action", "sizeBytes"],
      "properties": {
        "component": { "$ref": "#/$defs/identifier" },
        "relativePath": { "type": "string", "minLength": 1, "maxLength": 512 },
        "kind": { "enum": ["missing", "modified", "unexpected"] },
        "expectedSha256": { "oneOf": [{ "$ref": "#/$defs/sha256" }, { "type": "null" }] },
        "actualSha256": { "oneOf": [{ "$ref": "#/$defs/sha256" }, { "type": "null" }] },
        "action": { "enum": ["restore", "quarantine", "leave"] },
        "sizeBytes": { "type": "integer", "minimum": 0 }
      },
      "allOf": [
        {
          "if": { "properties": { "kind": { "const": "missing" } }, "required": ["kind"] },
          "then": { "properties": { "expectedSha256": { "$ref": "#/$defs/sha256" }, "actualSha256": { "type": "null" }, "action": { "const": "restore" } } }
        },
        {
          "if": { "properties": { "kind": { "const": "modified" } }, "required": ["kind"] },
          "then": { "properties": { "expectedSha256": { "$ref": "#/$defs/sha256" }, "actualSha256": { "$ref": "#/$defs/sha256" }, "action": { "const": "restore" } } }
        },
        {
          "if": { "properties": { "kind": { "const": "unexpected" } }, "required": ["kind"] },
          "then": { "properties": { "expectedSha256": { "type": "null" }, "actualSha256": { "$ref": "#/$defs/sha256" }, "action": { "enum": ["quarantine", "leave"] } } }
        }
      ]
    }
  }
}
