{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.kxm.dev/delivery-manifest.schema.json",
  "title": "KXM non-atomic multi-repository delivery manifest",
  "type": "object",
  "required": [
    "schema",
    "projectId",
    "runId",
    "createdAt",
    "configRevision",
    "atomic",
    "status",
    "repositories"
  ],
  "properties": {
    "schema": { "const": "kxm.delivery-manifest.v1" },
    "projectId": { "$ref": "common.schema.json#/$defs/opaqueId" },
    "runId": { "$ref": "common.schema.json#/$defs/opaqueId" },
    "createdAt": { "$ref": "common.schema.json#/$defs/timestamp" },
    "updatedAt": { "$ref": "common.schema.json#/$defs/timestamp" },
    "configRevision": { "$ref": "common.schema.json#/$defs/sha256" },
    "atomic": { "const": false },
    "status": { "enum": ["planned", "preparing", "delivering", "completed", "partial", "blocked_uncertain", "failed", "cancelled"] },
    "repositories": {
      "type": "array",
      "minItems": 1,
      "maxItems": 64,
      "items": { "$ref": "#/$defs/repositoryDelivery" }
    },
    "dependencies": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["before", "after"],
        "properties": {
          "before": { "$ref": "common.schema.json#/$defs/identifier" },
          "after": { "$ref": "common.schema.json#/$defs/identifier" },
          "reason": { "type": "string", "minLength": 1, "maxLength": 1000 }
        },
        "additionalProperties": false
      },
      "maxItems": 128
    },
    "summary": { "type": "string", "maxLength": 4000 }
  },
  "allOf": [
    {
      "if": { "properties": { "status": { "const": "completed" } }, "required": ["status"] },
      "then": {
        "properties": {
          "repositories": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": { "status": { "const": "delivered" } },
              "required": ["status", "receipts"]
            }
          }
        }
      }
    },
    {
      "if": { "properties": { "status": { "const": "partial" } }, "required": ["status"] },
      "then": {
        "properties": {
          "repositories": {
            "type": "array",
            "allOf": [
              { "contains": { "type": "object", "properties": { "status": { "const": "delivered" } }, "required": ["status"] } },
              { "contains": { "type": "object", "properties": { "status": { "enum": ["failed", "blocked_uncertain", "cancelled"] } }, "required": ["status"] } }
            ]
          }
        }
      }
    },
    {
      "if": { "properties": { "status": { "const": "cancelled" } }, "required": ["status"] },
      "then": {
        "properties": {
          "repositories": {
            "type": "array",
            "not": {
              "contains": { "type": "object", "properties": { "status": { "const": "delivered" } }, "required": ["status"] }
            }
          }
        }
      }
    },
    {
      "if": { "properties": { "status": { "const": "failed" } }, "required": ["status"] },
      "then": {
        "properties": {
          "repositories": {
            "type": "array",
            "contains": { "type": "object", "properties": { "status": { "const": "failed" } }, "required": ["status"] },
            "not": {
              "contains": { "type": "object", "properties": { "status": { "const": "delivered" } }, "required": ["status"] }
            }
          }
        }
      }
    },
    {
      "if": { "properties": { "status": { "const": "blocked_uncertain" } }, "required": ["status"] },
      "then": {
        "properties": {
          "repositories": {
            "type": "array",
            "contains": { "type": "object", "properties": { "status": { "const": "blocked_uncertain" } }, "required": ["status"] },
            "not": {
              "contains": { "type": "object", "properties": { "status": { "const": "delivered" } }, "required": ["status"] }
            }
          }
        }
      }
    }
  ],
  "additionalProperties": false,
  "$defs": {
    "repositoryDelivery": {
      "type": "object",
      "required": ["repositoryId", "baseCommit", "contentSnapshotHash", "status", "effect"],
      "properties": {
        "repositoryId": { "$ref": "common.schema.json#/$defs/identifier" },
        "baseCommit": { "$ref": "common.schema.json#/$defs/gitCommit" },
        "contentSnapshotHash": { "$ref": "common.schema.json#/$defs/sha256" },
        "outputCommit": { "$ref": "common.schema.json#/$defs/gitCommit" },
        "status": { "enum": ["pending", "prepared", "delivering", "delivered", "failed", "blocked_uncertain", "cancelled"] },
        "effect": { "$ref": "common.schema.json#/$defs/effectPolicy" },
        "target": {
          "type": "object",
          "required": ["kind"],
          "properties": {
            "kind": { "enum": ["local-commit", "branch", "pull-request", "artifact", "deployment"] },
            "ref": { "type": "string", "minLength": 1, "maxLength": 512 }
          },
          "additionalProperties": false
        },
        "receipts": {
          "type": "array",
          "items": { "$ref": "common.schema.json#/$defs/receipt" },
          "maxItems": 32
        },
        "error": { "$ref": "common.schema.json#/$defs/errorSummary" }
      },
      "allOf": [
        {
          "if": { "properties": { "status": { "const": "delivered" } }, "required": ["status"] },
          "then": {
            "properties": { "receipts": { "type": "array", "minItems": 1 } },
            "required": ["receipts"]
          }
        }
      ],
      "additionalProperties": false
    }
  }
}
