{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://postflight.co/aeronautiql/planning/v1_1/edits/receipt.schema.json",
  "title": "Planning edit receipt 1.1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "commandId",
    "documentId",
    "baseCommitId",
    "status",
    "reason",
    "commitId",
    "recordedAt",
    "durability"
  ],
  "properties": {
    "schemaVersion": {
      "const": "1.1"
    },
    "commandId": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/edits/command.schema.json#/$defs/id"
    },
    "documentId": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/edits/command.schema.json#/$defs/id"
    },
    "baseCommitId": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/edits/command.schema.json#/$defs/id"
    },
    "status": {
      "enum": [
        "accepted",
        "rejected",
        "conflict",
        "failed"
      ]
    },
    "reason": {
      "enum": [
        "applied",
        "malformedCommand",
        "commandIdReused",
        "invalidTimeWindow",
        "issuedInFuture",
        "expiredCommand",
        "wrongDocument",
        "staleBase",
        "targetMismatch",
        "expiredTarget",
        "nonInvertibleTarget",
        "legNotFound",
        "elementNotFound",
        "anchorNotFound",
        "duplicateElementId",
        "invalidMove",
        "invalidOperation",
        "noEffect",
        "invalidCandidate",
        "undoRecordNotFound",
        "undoPreconditionFailed",
        "alreadyUndone",
        "persistenceFailed"
      ]
    },
    "commitId": {
      "oneOf": [
        {
          "$ref": "https://postflight.co/aeronautiql/planning/v1/edits/command.schema.json#/$defs/id"
        },
        {
          "type": "null"
        }
      ]
    },
    "recordedAt": {
      "$ref": "https://postflight.co/aeronautiql/planning/v1/edits/command.schema.json#/$defs/timestamp"
    },
    "durability": {
      "enum": [
        "durable",
        "notDurable"
      ]
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "status": {
            "const": "accepted"
          }
        }
      },
      "then": {
        "properties": {
          "reason": {
            "const": "applied"
          },
          "commitId": {
            "$ref": "https://postflight.co/aeronautiql/planning/v1/edits/command.schema.json#/$defs/id"
          },
          "durability": {
            "const": "durable"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "const": "failed"
          }
        }
      },
      "then": {
        "properties": {
          "reason": {
            "const": "persistenceFailed"
          },
          "commitId": {
            "type": "null"
          },
          "durability": {
            "const": "notDurable"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "const": "conflict"
          }
        }
      },
      "then": {
        "properties": {
          "reason": {
            "const": "staleBase"
          },
          "commitId": {
            "$ref": "https://postflight.co/aeronautiql/planning/v1/edits/command.schema.json#/$defs/id"
          },
          "durability": {
            "const": "durable"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "const": "rejected"
          }
        }
      },
      "then": {
        "properties": {
          "reason": {
            "not": {
              "enum": [
                "applied",
                "staleBase",
                "persistenceFailed"
              ]
            }
          },
          "commitId": {
            "$ref": "https://postflight.co/aeronautiql/planning/v1/edits/command.schema.json#/$defs/id"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "reason": {
            "enum": [
              "malformedCommand",
              "commandIdReused"
            ]
          }
        },
        "required": [
          "reason"
        ]
      },
      "then": {
        "description": "Answered without a durable write: no record exists, and the receipt never claims durability.",
        "properties": {
          "status": {
            "const": "rejected"
          },
          "durability": {
            "const": "notDurable"
          }
        }
      },
      "else": {
        "if": {
          "properties": {
            "status": {
              "const": "rejected"
            }
          }
        },
        "then": {
          "properties": {
            "durability": {
              "const": "durable"
            }
          }
        }
      }
    }
  ]
}
