{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "overdeck.delivery/v1/receipt",
  "title": "Overdeck delivery receipt v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "contract_version",
    "status",
    "project",
    "environment",
    "run_id",
    "started_at",
    "completed_at",
    "source",
    "checks",
    "artifacts",
    "deployments",
    "rollback",
    "links",
    "completeness"
  ],
  "properties": {
    "contract_version": { "const": "overdeck.delivery/v1" },
    "status": {
      "enum": ["succeeded", "failed", "rolled_back", "rollback_failed", "skipped"]
    },
    "project": { "$ref": "#/$defs/id" },
    "environment": { "$ref": "#/$defs/id" },
    "run_id": { "type": "string", "minLength": 1 },
    "started_at": { "type": "string", "format": "date-time" },
    "completed_at": { "type": "string", "format": "date-time" },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["revision", "digest"],
      "properties": {
        "revision": { "type": "string", "minLength": 1 },
        "digest": { "$ref": "#/$defs/digest" }
      }
    },
    "checks": {
      "type": "object",
      "additionalProperties": false,
      "required": ["expected", "executed", "not_started", "results"],
      "properties": {
        "expected": { "$ref": "#/$defs/idList" },
        "executed": { "$ref": "#/$defs/idList" },
        "not_started": { "$ref": "#/$defs/idList" },
        "results": {
          "type": "array",
          "items": { "$ref": "#/$defs/checkResult" },
          "uniqueItemProperties": ["id"]
        }
      }
    },
    "artifacts": {
      "type": "array",
      "items": { "$ref": "#/$defs/artifact" }
    },
    "deployments": {
      "type": "array",
      "items": { "$ref": "#/$defs/deployment" }
    },
    "rollback": { "$ref": "#/$defs/rollback" },
    "links": {
      "type": "object",
      "additionalProperties": false,
      "required": ["receipt_uri", "log_uri", "provenance_uri"],
      "properties": {
        "receipt_uri": { "$ref": "#/$defs/uri" },
        "log_uri": { "$ref": "#/$defs/uri" },
        "provenance_uri": { "$ref": "#/$defs/uri" }
      }
    },
    "completeness": {
      "type": "object",
      "additionalProperties": false,
      "required": ["complete", "incomplete_reasons"],
      "properties": {
        "complete": { "type": "boolean" },
        "incomplete_reasons": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        }
      }
    },
    "migration": { "$ref": "#/$defs/migration" }
  },
  "deliveryReceipt": true,
  "allOf": [
    {
      "if": { "properties": { "status": { "const": "succeeded" } }, "required": ["status"] },
      "then": {
        "properties": {
          "checks": {
            "type": "object",
            "properties": {
              "not_started": { "maxItems": 0 },
              "results": {
                "items": {
                  "type": "object",
                  "properties": { "status": { "const": "succeeded" } },
                  "required": ["status"]
                }
              }
            }
          },
          "artifacts": { "minItems": 1 },
          "deployments": {
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "object",
              "properties": { "status": { "const": "succeeded" } },
              "required": ["status"]
            }
          },
          "completeness": {
            "type": "object",
            "properties": {
              "complete": { "const": true },
              "incomplete_reasons": { "maxItems": 0 }
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "id": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._/-]*$" },
    "idList": {
      "type": "array",
      "items": { "$ref": "#/$defs/id" },
      "uniqueItems": true
    },
    "uri": { "type": "string", "format": "uri", "minLength": 1 },
    "digest": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9+._-]*:[A-Fa-f0-9]{32,}$"
    },
    "checkResult": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "status"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "status": { "enum": ["succeeded", "failed", "skipped", "timed_out", "cancelled"] },
        "summary": { "type": "string" }
      }
    },
    "artifact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["uri", "digest", "media_type", "producer"],
      "properties": {
        "uri": { "$ref": "#/$defs/uri" },
        "digest": { "$ref": "#/$defs/digest" },
        "media_type": { "type": "string", "minLength": 1 },
        "producer": { "type": "string", "minLength": 1 }
      }
    },
    "deployment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "unit",
        "status",
        "target",
        "artifact_digest",
        "provider",
        "desired_identity",
        "observed_identity",
        "previous_known_good",
        "rollback"
      ],
      "properties": {
        "unit": { "$ref": "#/$defs/id" },
        "status": { "enum": ["succeeded", "failed", "rolled_back", "skipped"] },
        "target": { "type": "string", "minLength": 1 },
        "artifact_digest": { "$ref": "#/$defs/digest" },
        "provider": {
          "type": "object",
          "additionalProperties": false,
          "required": ["name", "deployment_id", "deployment_url"],
          "properties": {
            "name": { "type": "string", "minLength": 1 },
            "deployment_id": { "type": "string", "minLength": 1 },
            "deployment_url": { "$ref": "#/$defs/uri" },
            "version_id": { "type": "string", "minLength": 1 },
            "version_url": { "$ref": "#/$defs/uri" }
          }
        },
        "desired_identity": { "type": "string", "minLength": 1 },
        "observed_identity": { "type": "string", "minLength": 1 },
        "previous_known_good": {
          "type": "object",
          "additionalProperties": false,
          "required": ["identity"],
          "properties": {
            "identity": { "type": "string", "minLength": 1 },
            "deployment_id": { "type": "string", "minLength": 1 },
            "deployment_url": { "$ref": "#/$defs/uri" }
          }
        },
        "rollback": { "$ref": "#/$defs/rollback" }
      }
    },
    "rollback": {
      "type": "object",
      "additionalProperties": false,
      "required": ["supported", "action", "status"],
      "properties": {
        "supported": { "type": "boolean" },
        "action": { "enum": ["none", "attempted", "compensated"] },
        "status": { "enum": ["not_required", "not_attempted", "succeeded", "failed", "unsupported"] }
      },
      "allOf": [
        {
          "if": { "properties": { "supported": { "const": false } }, "required": ["supported"] },
          "then": {
            "properties": {
              "action": { "const": "none" },
              "status": { "const": "unsupported" }
            }
          }
        },
        {
          "if": {
            "properties": {
              "supported": { "const": true },
              "action": { "const": "none" }
            },
            "required": ["supported", "action"]
          },
          "then": { "properties": { "status": { "enum": ["not_required", "not_attempted"] } } }
        },
        {
          "if": { "properties": { "action": { "enum": ["attempted", "compensated"] } }, "required": ["action"] },
          "then": { "properties": { "status": { "enum": ["succeeded", "failed"] } } }
        }
      ]
    },
    "migration": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "compensation"],
      "properties": {
        "status": { "enum": ["not_run", "succeeded", "failed"] },
        "compensation": { "$ref": "#/$defs/rollback" }
      }
    }
  }
}
