{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:junexis:schema:migration-plan:0.1",
  "title": "Plan migracji danych Junexis 0.1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "migrationId", "component", "fromVersion", "toVersion", "strategy", "reverseMigrationId", "backup", "explicitConsentRequired", "preconditions"],
  "properties": {
    "schemaVersion": { "const": "junexis.migration-plan/0.1" },
    "migrationId": { "$ref": "#/$defs/identifier" },
    "component": { "$ref": "#/$defs/identifier" },
    "fromVersion": { "$ref": "#/$defs/version" },
    "toVersion": { "$ref": "#/$defs/version" },
    "strategy": { "enum": ["reversible", "restore-backup", "irreversible"] },
    "reverseMigrationId": { "oneOf": [{ "$ref": "#/$defs/identifier" }, { "type": "null" }] },
    "backup": { "$ref": "#/$defs/backup" },
    "explicitConsentRequired": { "type": "boolean" },
    "preconditions": {
      "type": "array",
      "maxItems": 128,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/identifier" }
    }
  },
  "allOf": [
    {
      "if": { "properties": { "strategy": { "const": "reversible" } }, "required": ["strategy"] },
      "then": { "properties": { "reverseMigrationId": { "$ref": "#/$defs/identifier" } } }
    },
    {
      "if": { "properties": { "strategy": { "enum": ["restore-backup", "irreversible"] } }, "required": ["strategy"] },
      "then": {
        "properties": {
          "reverseMigrationId": { "type": "null" },
          "backup": { "properties": { "required": { "const": true } } }
        }
      }
    },
    {
      "if": { "properties": { "strategy": { "const": "irreversible" } }, "required": ["strategy"] },
      "then": { "properties": { "explicitConsentRequired": { "const": true } } }
    }
  ],
  "$defs": {
    "identifier": { "type": "string", "pattern": "^[a-z0-9][a-z0-9._-]{1,127}$" },
    "version": { "type": "string", "minLength": 1, "maxLength": 128 },
    "backup": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "estimatedBytes", "retention"],
      "properties": {
        "required": { "type": "boolean" },
        "estimatedBytes": { "type": "integer", "minimum": 0 },
        "retention": { "enum": ["until-commit", "until-next-update", "manual-removal"] }
      }
    }
  }
}
