{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:sitla:lifecycle:1:receipt",
  "$comment": "SPDX-License-Identifier: CC0-1.0; static exchange shape, not authorization or proof of erasure",
  "type": "object",
  "properties": {
    "protocol": {
      "const": "sitla-wire/1"
    },
    "profile": {
      "const": "sitla-account-lifecycle/1"
    },
    "domain": {
      "const": "SITLA/lifecycle-receipt/1"
    },
    "operation": {
      "enum": [
        "logout",
        "logout-all",
        "revoke-mandate",
        "delete-account"
      ]
    },
    "operator_id": {
      "type": "string",
      "pattern": "^https://[a-z0-9.-]+$"
    },
    "account_id": {
      "type": "string",
      "pattern": "^https://[a-z0-9.-]+/accounts/[A-Za-z0-9_-]{22}$"
    },
    "request_id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2048
    },
    "request_digest": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "audience": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2048
    },
    "mandate_id": {
      "anyOf": [
        {
          "type": "string",
          "minLength": 1,
          "maxLength": 2048
        },
        {
          "type": "null"
        }
      ]
    },
    "state": {
      "enum": [
        "accepted",
        "processing",
        "completed",
        "rejected"
      ]
    },
    "recorded_at": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
    },
    "effective_at": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
        },
        {
          "type": "null"
        }
      ]
    },
    "completed_at": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
        },
        {
          "type": "null"
        }
      ]
    },
    "session_epoch": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9007199254740991
    },
    "session_scope": {
      "enum": [
        "none",
        "current-family",
        "all-families"
      ]
    },
    "mandate_scope": {
      "enum": [
        "none",
        "target-and-descendants",
        "all-owned-and-descendants"
      ]
    },
    "account_state": {
      "enum": [
        "active",
        "deletion-pending",
        "deleted"
      ]
    },
    "credential_scope": {
      "enum": [
        "unchanged",
        "suspended",
        "revoked"
      ]
    },
    "status_ref": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^https://[^\\s]+$"
        },
        {
          "type": "null"
        }
      ]
    },
    "retained_records": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "category": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128
          },
          "purpose": {
            "type": "string",
            "minLength": 1,
            "maxLength": 512
          },
          "basis": {
            "type": "string",
            "minLength": 1,
            "maxLength": 512
          },
          "retention_until": {
            "anyOf": [
              {
                "type": "string",
                "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
              },
              {
                "type": "null"
              }
            ]
          },
          "retention_criteria": {
            "anyOf": [
              {
                "type": "string",
                "minLength": 1,
                "maxLength": 512
              },
              {
                "type": "null"
              }
            ]
          }
        },
        "required": [
          "category",
          "purpose",
          "basis",
          "retention_until",
          "retention_criteria"
        ],
        "additionalProperties": false,
        "allOf": [
          {
            "anyOf": [
              {
                "properties": {
                  "retention_until": {
                    "type": "string"
                  }
                }
              },
              {
                "properties": {
                  "retention_criteria": {
                    "type": "string"
                  }
                }
              }
            ]
          }
        ]
      },
      "maxItems": 64
    },
    "error": {
      "anyOf": [
        {
          "enum": [
            "not_authorized",
            "reauthentication_required",
            "recovery_restricted",
            "account_inactive",
            "mandate_not_found",
            "idempotency_conflict",
            "request_expired",
            "epoch_mismatch",
            "temporarily_unavailable",
            "operation_not_allowed"
          ]
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "required": [
    "protocol",
    "profile",
    "domain",
    "operation",
    "operator_id",
    "account_id",
    "request_id",
    "request_digest",
    "audience",
    "mandate_id",
    "state",
    "recorded_at",
    "effective_at",
    "completed_at",
    "session_epoch",
    "session_scope",
    "mandate_scope",
    "account_state",
    "credential_scope",
    "status_ref",
    "retained_records",
    "error"
  ],
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "properties": {
          "state": {
            "const": "rejected"
          }
        }
      },
      "then": {
        "properties": {
          "session_scope": {
            "const": "none"
          },
          "mandate_scope": {
            "const": "none"
          },
          "credential_scope": {
            "const": "unchanged"
          },
          "effective_at": {
            "type": "null"
          },
          "completed_at": {
            "type": "null"
          },
          "retained_records": {
            "type": "array",
            "maxItems": 0
          },
          "error": {
            "type": "string"
          }
        }
      },
      "else": {
        "properties": {
          "error": {
            "type": "null"
          },
          "effective_at": {
            "type": "string",
            "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "state": {
            "const": "completed"
          }
        }
      },
      "then": {
        "properties": {
          "completed_at": {
            "type": "string",
            "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
          }
        }
      },
      "else": {
        "properties": {
          "completed_at": {
            "type": "null"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "state": {
            "enum": [
              "accepted",
              "processing"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "operation": {
            "const": "delete-account"
          },
          "status_ref": {
            "type": "string"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "operation": {
            "const": "revoke-mandate"
          }
        }
      },
      "then": {
        "properties": {
          "mandate_id": {
            "type": "string",
            "minLength": 1,
            "maxLength": 2048
          }
        }
      },
      "else": {
        "properties": {
          "mandate_id": {
            "type": "null"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "operation": {
            "const": "logout"
          },
          "state": {
            "not": {
              "const": "rejected"
            }
          }
        }
      },
      "then": {
        "properties": {
          "state": {
            "const": "completed"
          },
          "session_scope": {
            "const": "current-family"
          },
          "mandate_scope": {
            "const": "none"
          },
          "account_state": {
            "const": "active"
          },
          "credential_scope": {
            "const": "unchanged"
          },
          "retained_records": {
            "type": "array",
            "maxItems": 0
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "operation": {
            "const": "logout-all"
          },
          "state": {
            "not": {
              "const": "rejected"
            }
          }
        }
      },
      "then": {
        "properties": {
          "state": {
            "const": "completed"
          },
          "session_scope": {
            "const": "all-families"
          },
          "mandate_scope": {
            "const": "none"
          },
          "account_state": {
            "const": "active"
          },
          "credential_scope": {
            "const": "unchanged"
          },
          "retained_records": {
            "type": "array",
            "maxItems": 0
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "operation": {
            "const": "revoke-mandate"
          },
          "state": {
            "not": {
              "const": "rejected"
            }
          }
        }
      },
      "then": {
        "properties": {
          "state": {
            "const": "completed"
          },
          "session_scope": {
            "const": "none"
          },
          "mandate_scope": {
            "const": "target-and-descendants"
          },
          "account_state": {
            "const": "active"
          },
          "credential_scope": {
            "const": "unchanged"
          },
          "retained_records": {
            "type": "array",
            "maxItems": 0
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "operation": {
            "const": "delete-account"
          },
          "state": {
            "not": {
              "const": "rejected"
            }
          }
        }
      },
      "then": {
        "properties": {
          "session_scope": {
            "const": "all-families"
          },
          "mandate_scope": {
            "const": "all-owned-and-descendants"
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "state": {
                  "const": "completed"
                }
              }
            },
            "then": {
              "properties": {
                "account_state": {
                  "const": "deleted"
                },
                "credential_scope": {
                  "const": "revoked"
                }
              }
            },
            "else": {
              "properties": {
                "account_state": {
                  "const": "deletion-pending"
                },
                "credential_scope": {
                  "const": "suspended"
                }
              }
            }
          }
        ]
      }
    }
  ]
}
