{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:junexis:schema:rollback-plan:0.1",
  "title": "Plan powrotu Junexis 0.1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "planId", "operation", "createdAt", "rid", "reason", "expectedGeneration", "targetGeneration", "currentRelease", "targetRelease", "result", "blockers", "targetValidation", "dataActions", "healthCheckRequired"],
  "properties": {
    "schemaVersion": { "const": "junexis.rollback-plan/0.1" },
    "planId": { "$ref": "#/$defs/identifier" },
    "operation": { "const": "rollback" },
    "createdAt": { "$ref": "#/$defs/timestamp" },
    "rid": { "enum": ["win-x64", "linux-x64", "osx-arm64"] },
    "reason": { "enum": ["user-requested", "activation-failed", "health-failed", "migration-failed", "recovery"] },
    "expectedGeneration": { "type": "integer", "minimum": 1 },
    "targetGeneration": { "type": "integer", "minimum": 2 },
    "currentRelease": { "$ref": "#/$defs/release" },
    "targetRelease": { "$ref": "#/$defs/release" },
    "result": { "enum": ["ready", "blocked"] },
    "blockers": { "type": "array", "maxItems": 128, "items": { "$ref": "#/$defs/blocker" } },
    "targetValidation": { "$ref": "#/$defs/validation" },
    "dataActions": { "type": "array", "maxItems": 256, "items": { "$ref": "#/$defs/dataAction" } },
    "healthCheckRequired": { "const": true }
  },
  "allOf": [
    {
      "if": { "properties": { "result": { "const": "ready" } }, "required": ["result"] },
      "then": {
        "properties": {
          "blockers": { "maxItems": 0 },
          "targetValidation": {
            "properties": {
              "installed": { "const": true },
              "integrityVerified": { "const": true },
              "compatible": { "const": true },
              "revoked": { "const": false }
            }
          }
        }
      }
    },
    {
      "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 }
      }
    },
    "validation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["installed", "integrityVerified", "compatible", "revoked"],
      "properties": {
        "installed": { "type": "boolean" },
        "integrityVerified": { "type": "boolean" },
        "compatible": { "type": "boolean" },
        "revoked": { "type": "boolean" }
      }
    },
    "dataAction": {
      "type": "object",
      "additionalProperties": false,
      "required": ["component", "migrationId", "action"],
      "properties": {
        "component": { "$ref": "#/$defs/identifier" },
        "migrationId": { "$ref": "#/$defs/identifier" },
        "action": { "enum": ["reverse-migration", "restore-backup", "not-required"] }
      }
    }
  }
}
