{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://postflight.co/aeronautiql/planning/v1/edits/receipt.schema.json",
  "title": "Planning edit receipt v1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "commandId", "documentId", "baseCommitId", "status", "reason", "commitId", "recordedAt", "durability"],
  "properties": {
    "schemaVersion": { "const": "1.0" },
    "commandId": { "$ref": "command.schema.json#/$defs/id" },
    "documentId": { "$ref": "command.schema.json#/$defs/id" },
    "baseCommitId": { "$ref": "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": "command.schema.json#/$defs/id" }, { "type": "null" }] },
    "recordedAt": { "$ref": "command.schema.json#/$defs/timestamp" },
    "durability": { "enum": ["durable", "notDurable"] }
  },
  "allOf": [
    {
      "if": { "properties": { "status": { "const": "accepted" } } },
      "then": {
        "properties": {
          "reason": { "const": "applied" },
          "commitId": { "$ref": "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": "command.schema.json#/$defs/id" },
          "durability": { "const": "durable" }
        }
      }
    },
    {
      "if": { "properties": { "status": { "const": "rejected" } } },
      "then": {
        "properties": {
          "reason": { "not": { "enum": ["applied", "staleBase", "persistenceFailed"] } },
          "commitId": { "$ref": "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" } } }
      }
    }
  ]
}
