{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:junexis:schema:installation-state:0.1",
  "title": "Stan instalacji Junexis 0.1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "generation", "status", "rid", "activeRelease", "installedReleases", "currentOperationId", "lastCommittedOperationId", "updatedAt"],
  "properties": {
    "schemaVersion": { "const": "junexis.installation-state/0.1" },
    "generation": { "type": "integer", "minimum": 0 },
    "status": { "enum": ["not-installed", "ready", "changing", "recovery-required"] },
    "rid": { "enum": ["win-x64", "linux-x64", "osx-arm64"] },
    "activeRelease": { "oneOf": [{ "$ref": "#/$defs/release" }, { "type": "null" }] },
    "installedReleases": {
      "type": "array",
      "maxItems": 64,
      "items": { "$ref": "#/$defs/installedRelease" }
    },
    "currentOperationId": { "oneOf": [{ "$ref": "#/$defs/identifier" }, { "type": "null" }] },
    "lastCommittedOperationId": { "oneOf": [{ "$ref": "#/$defs/identifier" }, { "type": "null" }] },
    "updatedAt": { "$ref": "#/$defs/timestamp" }
  },
  "allOf": [
    {
      "if": { "properties": { "status": { "const": "not-installed" } }, "required": ["status"] },
      "then": {
        "properties": {
          "generation": { "const": 0 },
          "activeRelease": { "type": "null" },
          "installedReleases": { "maxItems": 0 },
          "currentOperationId": { "type": "null" },
          "lastCommittedOperationId": { "type": "null" }
        }
      }
    },
    {
      "if": { "properties": { "status": { "const": "ready" } }, "required": ["status"] },
      "then": {
        "properties": {
          "generation": { "minimum": 1 },
          "activeRelease": { "$ref": "#/$defs/release" },
          "installedReleases": { "minItems": 1 },
          "currentOperationId": { "type": "null" },
          "lastCommittedOperationId": { "$ref": "#/$defs/identifier" }
        }
      }
    },
    {
      "if": { "properties": { "status": { "const": "changing" } }, "required": ["status"] },
      "then": { "properties": { "currentOperationId": { "$ref": "#/$defs/identifier" } } }
    }
  ],
  "$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" }
      }
    },
    "installedRelease": {
      "type": "object",
      "additionalProperties": false,
      "required": ["release", "role", "installedAt"],
      "properties": {
        "release": { "$ref": "#/$defs/release" },
        "role": { "enum": ["active", "rollback", "inactive", "quarantined"] },
        "installedAt": { "$ref": "#/$defs/timestamp" }
      }
    }
  }
}
