{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://w3id.org/modavis/vao/0.5.0/schema/materialization-receipt.json",
  "title": "VAO 0.5.0 runtime materialization receipt",
  "type": "object",
  "required": [
    "$schema",
    "type",
    "formatVersion",
    "releaseId",
    "manifestSHA256",
    "instanceId",
    "createdAt",
    "implementation",
    "sourceCarrier",
    "selectedGroupIds",
    "acquisitions",
    "profileStates"
  ],
  "properties": {
    "$schema": {
      "const": "https://w3id.org/modavis/vao/0.5.0/schema/materialization-receipt.json"
    },
    "type": {
      "const": "VAOMaterializationReceipt"
    },
    "formatVersion": {
      "const": "0.5.0"
    },
    "releaseId": {
      "$ref": "#/$defs/id"
    },
    "manifestSHA256": {
      "$ref": "#/$defs/sha"
    },
    "instanceId": {
      "$ref": "#/$defs/id"
    },
    "createdAt": {
      "$ref": "#/$defs/dateTime"
    },
    "implementation": {
      "$ref": "#/$defs/implementation"
    },
    "sourceCarrier": {
      "$ref": "#/$defs/sourceCarrier"
    },
    "selectedGroupIds": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "$ref": "#/$defs/id"
      }
    },
    "acquisitions": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "realizationId",
          "distributionId",
          "status",
          "attemptedAt"
        ],
        "properties": {
          "realizationId": {
            "$ref": "#/$defs/id"
          },
          "distributionId": {
            "$ref": "#/$defs/id"
          },
          "byteSize": {
            "description": "Observed acquired byte count; required for verified data and optional evidence for an integrity failure.",
            "type": "integer",
            "minimum": 0,
            "maximum": 9007199254740991
          },
          "sha256": {
            "description": "SHA-256 of observed acquired bytes; required for verified data and optional evidence for an integrity failure.",
            "$ref": "#/$defs/sha"
          },
          "status": {
            "enum": [
              "verified",
              "integrity-failed",
              "unavailable",
              "authentication-required",
              "policy-blocked"
            ]
          },
          "attemptedAt": {
            "description": "Instant at which the acquisition attempt began or was issued.",
            "$ref": "#/$defs/dateTime"
          },
          "verifiedAt": {
            "description": "Instant at which acquired bytes successfully matched the immutable realization identity.",
            "$ref": "#/$defs/dateTime"
          },
          "diagnostic": {
            "description": "Bounded human-readable failure evidence; it must not contain credentials or other secrets.",
            "type": "string",
            "minLength": 1,
            "maxLength": 4096
          }
        },
        "additionalProperties": false,
        "allOf": [
          {
            "if": {
              "properties": {
                "status": {
                  "const": "verified"
                }
              },
              "required": [
                "status"
              ]
            },
            "then": {
              "required": [
                "byteSize",
                "sha256",
                "verifiedAt"
              ]
            },
            "else": {
              "not": {
                "required": [
                  "verifiedAt"
                ]
              },
              "required": [
                "diagnostic"
              ]
            }
          },
          {
            "if": {
              "properties": {
                "status": {
                  "enum": [
                    "unavailable",
                    "authentication-required",
                    "policy-blocked"
                  ]
                }
              },
              "required": [
                "status"
              ]
            },
            "then": {
              "not": {
                "anyOf": [
                  {
                    "required": [
                      "byteSize"
                    ]
                  },
                  {
                    "required": [
                      "sha256"
                    ]
                  }
                ]
              }
            }
          },
          {
            "if": {
              "properties": {
                "status": {
                  "const": "integrity-failed"
                }
              },
              "required": [
                "status"
              ]
            },
            "then": {
              "anyOf": [
                {
                  "required": [
                    "byteSize"
                  ]
                },
                {
                  "required": [
                    "sha256"
                  ]
                }
              ]
            }
          }
        ]
      }
    },
    "profileStates": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "profileId",
          "state"
        ],
        "properties": {
          "profileId": {
            "type": "string",
            "format": "uri"
          },
          "state": {
            "enum": [
              "embedded-valid",
              "materialized-valid",
              "incomplete",
              "unavailable",
              "integrity-failed",
              "policy-blocked"
            ]
          }
        },
        "additionalProperties": false
      }
    }
  },
  "additionalProperties": false,
  "$defs": {
    "dateTime": {
      "description": "RFC 3339 instant in the VAO exact-comparison subset: known offset, seconds 00-59, and at most 18 fractional digits.",
      "type": "string",
      "format": "date-time",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}[Tt][0-9]{2}:[0-9]{2}:[0-5][0-9](?:\\.[0-9]{1,18})?(?:[Zz]|[+-][0-9]{2}:[0-9]{2})$",
      "not": {
        "pattern": "-00:00$"
      }
    },
    "id": {
      "type": "string",
      "pattern": "^(urn:|https?:)[^\\s]+$"
    },
    "sha": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "sourceCarrier": {
      "description": "Exact evidence identifying the carrier descriptor used for materialization and, for a packed carrier, the complete container bytes.",
      "type": "object",
      "required": [
        "kind",
        "descriptorByteSize",
        "descriptorSHA256"
      ],
      "properties": {
        "kind": {
          "enum": [
            "workspace",
            "packed-carrier"
          ]
        },
        "descriptorByteSize": {
          "type": "integer",
          "minimum": 0,
          "maximum": 9007199254740991
        },
        "descriptorSHA256": {
          "$ref": "#/$defs/sha"
        },
        "packedCarrierByteSize": {
          "type": "integer",
          "minimum": 0,
          "maximum": 9007199254740991
        },
        "packedCarrierSHA256": {
          "$ref": "#/$defs/sha"
        }
      },
      "additionalProperties": false,
      "if": {
        "properties": {
          "kind": {
            "const": "packed-carrier"
          }
        },
        "required": [
          "kind"
        ]
      },
      "then": {
        "required": [
          "packedCarrierByteSize",
          "packedCarrierSHA256"
        ]
      },
      "else": {
        "not": {
          "anyOf": [
            {
              "required": [
                "packedCarrierByteSize"
              ]
            },
            {
              "required": [
                "packedCarrierSHA256"
              ]
            }
          ]
        }
      }
    },
    "implementation": {
      "type": "object",
      "required": [
        "name",
        "version",
        "identity",
        "identityScope",
        "identityDescription"
      ],
      "properties": {
        "softwareEnvironmentId": {
          "$ref": "#/$defs/id"
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "version": {
          "type": "string",
          "minLength": 1
        },
        "identity": {
          "type": "object",
          "required": [
            "algorithm",
            "value"
          ],
          "properties": {
            "algorithm": {
              "const": "sha256"
            },
            "value": {
              "$ref": "#/$defs/sha"
            }
          },
          "additionalProperties": false
        },
        "identityScope": {
          "enum": [
            "executable",
            "source-file",
            "source-bundle",
            "container-image"
          ]
        },
        "identityDescription": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": false
    }
  }
}
