{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:junexis:schema:update-plan:0.1",
  "title": "Plan aktualizacji Junexis 0.1",
  "description": "Publiczny, tylko do odczytu wynik planowania aktualizacji jednego profilu Junexis.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion", "planId", "operation", "createdAt", "channel", "rid",
    "catalog", "currentRelease", "targetRelease", "result", "blockers",
    "components", "totals", "rollback"
  ],
  "properties": {
    "schemaVersion": { "const": "junexis.update-plan/0.1" },
    "planId": { "$ref": "#/$defs/identifier" },
    "operation": { "const": "update" },
    "createdAt": { "$ref": "#/$defs/timestamp" },
    "channel": { "enum": ["dev", "preview", "stable"] },
    "rid": { "enum": ["win-x64", "linux-x64", "osx-arm64"] },
    "catalog": { "$ref": "#/$defs/catalogReference" },
    "currentRelease": { "$ref": "#/$defs/releaseReference" },
    "targetRelease": { "$ref": "#/$defs/releaseReference" },
    "result": { "enum": ["ready", "no-change", "blocked"] },
    "blockers": {
      "type": "array",
      "maxItems": 128,
      "items": { "$ref": "#/$defs/blocker" }
    },
    "components": {
      "type": "array",
      "maxItems": 256,
      "items": { "$ref": "#/$defs/componentChange" }
    },
    "totals": { "$ref": "#/$defs/totals" },
    "rollback": { "$ref": "#/$defs/rollback" }
  },
  "allOf": [
    {
      "if": { "properties": { "result": { "const": "ready" } }, "required": ["result"] },
      "then": {
        "properties": {
          "blockers": { "maxItems": 0 },
          "components": { "minItems": 1 },
          "rollback": { "properties": { "available": { "const": true } } }
        }
      }
    },
    {
      "if": { "properties": { "result": { "const": "no-change" } }, "required": ["result"] },
      "then": {
        "properties": {
          "blockers": { "maxItems": 0 },
          "components": { "maxItems": 0 },
          "totals": {
            "properties": {
              "downloadBytes": { "const": 0 },
              "requiredFreeBytes": { "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$" },
    "releaseReference": {
      "type": "object",
      "additionalProperties": false,
      "required": ["releaseId", "version", "manifestSha256"],
      "properties": {
        "releaseId": { "$ref": "#/$defs/identifier" },
        "version": { "$ref": "#/$defs/semver" },
        "manifestSha256": { "$ref": "#/$defs/sha256" }
      }
    },
    "catalogReference": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schemaVersion", "issuedAt", "sha256"],
      "properties": {
        "schemaVersion": { "const": "junexis.platform-release-catalog/0.2" },
        "issuedAt": { "$ref": "#/$defs/timestamp" },
        "sha256": { "$ref": "#/$defs/sha256" }
      }
    },
    "blocker": {
      "type": "object",
      "additionalProperties": false,
      "required": ["code", "message"],
      "properties": {
        "code": { "$ref": "#/$defs/identifier" },
        "message": { "type": "string", "minLength": 1, "maxLength": 512 }
      }
    },
    "dependency": {
      "type": "object",
      "additionalProperties": false,
      "required": ["component", "version"],
      "properties": {
        "component": { "$ref": "#/$defs/identifier" },
        "version": { "$ref": "#/$defs/semver" }
      }
    },
    "artifact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["rid", "format", "url", "sizeBytes", "sha256"],
      "properties": {
        "rid": { "enum": ["win-x64", "linux-x64", "osx-arm64"] },
        "format": { "enum": ["zip", "tar.gz", "pkg", "vsix", "nupkg"] },
        "url": { "type": "string", "pattern": "^https://[^\\s]+$" },
        "sizeBytes": { "type": "integer", "minimum": 1 },
        "sha256": { "$ref": "#/$defs/sha256" }
      }
    },
    "componentChange": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "component", "fromVersion", "toVersion", "action", "required",
        "dependencies", "migrationIds", "artifact"
      ],
      "properties": {
        "component": { "$ref": "#/$defs/identifier" },
        "fromVersion": { "oneOf": [{ "$ref": "#/$defs/semver" }, { "type": "null" }] },
        "toVersion": { "oneOf": [{ "$ref": "#/$defs/semver" }, { "type": "null" }] },
        "action": { "enum": ["install", "upgrade", "downgrade", "keep", "remove"] },
        "required": { "type": "boolean" },
        "dependencies": {
          "type": "array",
          "maxItems": 128,
          "items": { "$ref": "#/$defs/dependency" }
        },
        "migrationIds": {
          "type": "array",
          "maxItems": 128,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/identifier" }
        },
        "artifact": { "oneOf": [{ "$ref": "#/$defs/artifact" }, { "type": "null" }] }
      },
      "allOf": [
        {
          "if": { "properties": { "action": { "const": "install" } }, "required": ["action"] },
          "then": {
            "properties": {
              "fromVersion": { "type": "null" },
              "toVersion": { "$ref": "#/$defs/semver" },
              "artifact": { "$ref": "#/$defs/artifact" }
            }
          }
        },
        {
          "if": { "properties": { "action": { "enum": ["upgrade", "downgrade"] } }, "required": ["action"] },
          "then": {
            "properties": {
              "fromVersion": { "$ref": "#/$defs/semver" },
              "toVersion": { "$ref": "#/$defs/semver" },
              "artifact": { "$ref": "#/$defs/artifact" }
            }
          }
        },
        {
          "if": { "properties": { "action": { "const": "keep" } }, "required": ["action"] },
          "then": {
            "properties": {
              "fromVersion": { "$ref": "#/$defs/semver" },
              "toVersion": { "$ref": "#/$defs/semver" },
              "artifact": { "type": "null" }
            }
          }
        },
        {
          "if": { "properties": { "action": { "const": "remove" } }, "required": ["action"] },
          "then": {
            "properties": {
              "fromVersion": { "$ref": "#/$defs/semver" },
              "toVersion": { "type": "null" },
              "artifact": { "type": "null" }
            }
          }
        }
      ]
    },
    "totals": {
      "type": "object",
      "additionalProperties": false,
      "required": ["downloadBytes", "requiredFreeBytes"],
      "properties": {
        "downloadBytes": { "type": "integer", "minimum": 0 },
        "requiredFreeBytes": { "type": "integer", "minimum": 0 }
      }
    },
    "rollback": {
      "type": "object",
      "additionalProperties": false,
      "required": ["available", "targetRelease", "dataStrategy", "estimatedBackupBytes"],
      "properties": {
        "available": { "type": "boolean" },
        "targetRelease": { "oneOf": [{ "$ref": "#/$defs/releaseReference" }, { "type": "null" }] },
        "dataStrategy": { "enum": ["not-required", "reverse-migration", "restore-backup"] },
        "estimatedBackupBytes": { "type": "integer", "minimum": 0 }
      },
      "allOf": [
        {
          "if": { "properties": { "available": { "const": true } }, "required": ["available"] },
          "then": { "properties": { "targetRelease": { "$ref": "#/$defs/releaseReference" } } }
        },
        {
          "if": { "properties": { "available": { "const": false } }, "required": ["available"] },
          "then": { "properties": { "targetRelease": { "type": "null" } } }
        }
      ]
    }
  }
}
