{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paulkov.github.io/dpone/schemas/gitops/deployment-cache-retention-plan.schema.json",
  "title": "dpone GitOps deployment cache retention plan",
  "type": "object",
  "required": [
    "schema",
    "environment",
    "current_deployment_id",
    "protected_deployment_ids",
    "items",
    "delete_candidates"
  ],
  "additionalProperties": true,
  "properties": {
    "schema": {
      "const": "dpone.deployment-cache-retention-plan.v1"
    },
    "environment": {
      "type": "string",
      "minLength": 1
    },
    "current_deployment_id": {
      "anyOf": [
        {
          "$ref": "#/$defs/sha256"
        },
        {
          "type": "null"
        }
      ]
    },
    "protected_deployment_ids": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/sha256"
      },
      "uniqueItems": true
    },
    "items": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/item"
      }
    },
    "delete_candidates": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/sha256"
      }
    }
  },
  "$defs": {
    "sha256": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "item": {
      "type": "object",
      "required": [
        "deployment_id",
        "action",
        "reason",
        "path"
      ],
      "additionalProperties": true,
      "properties": {
        "deployment_id": {
          "anyOf": [
            {
              "$ref": "#/$defs/sha256"
            },
            {
              "type": "null"
            }
          ]
        },
        "action": {
          "enum": [
            "protect",
            "delete",
            "quarantine"
          ]
        },
        "reason": {
          "enum": [
            "current",
            "retention_evidence",
            "unreferenced",
            "incomplete",
            "invalid"
          ]
        },
        "path": {
          "type": "string",
          "minLength": 1
        },
        "error_code": {
          "type": "string",
          "pattern": "^DPONE_(CACHE|RELEASE|DEPLOYMENT|AIRFLOW)_[A-Z0-9_]+$"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "deployment_id": {
                "type": "null"
              }
            },
            "required": [
              "deployment_id"
            ]
          },
          "then": {
            "properties": {
              "action": {
                "const": "quarantine"
              },
              "reason": {
                "enum": [
                  "incomplete",
                  "invalid"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "action": {
                "enum": [
                  "protect",
                  "delete"
                ]
              }
            },
            "required": [
              "action"
            ]
          },
          "then": {
            "properties": {
              "deployment_id": {
                "$ref": "#/$defs/sha256"
              }
            }
          }
        }
      ]
    }
  }
}
