{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://pipelinexlab.com/contracts/document-application-snapshot.v1.schema.json",
  "title": "Reviewed Report application snapshot V1",
  "description": "Detached public application view. Runtime owns review, exact Plan validation, authority and atomic receipt publication.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "commandId",
    "jobRef",
    "resultRef",
    "target",
    "state",
    "receipt",
    "diagnostics"
  ],
  "properties": {
    "schemaVersion": {
      "const": 1
    },
    "commandId": {
      "type": "string",
      "minLength": 1
    },
    "jobRef": {
      "type": "string",
      "minLength": 1
    },
    "resultRef": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "target": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "scope",
        "reportKey",
        "revisionRef"
      ],
      "properties": {
        "scope": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind",
            "workspaceKey",
            "projectKey"
          ],
          "properties": {
            "kind": {
              "const": "project"
            },
            "workspaceKey": {
              "type": "string",
              "minLength": 1
            },
            "projectKey": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        "reportKey": {
          "type": "string",
          "minLength": 1
        },
        "revisionRef": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        }
      }
    },
    "state": {
      "enum": [
        "pending",
        "applied",
        "refused"
      ]
    },
    "receipt": {
      "anyOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "applied",
            "target",
            "newRevisionRef",
            "newGeneration",
            "semanticDigest"
          ],
          "properties": {
            "applied": {
              "const": true
            },
            "target": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "scope",
                "reportKey",
                "revisionRef"
              ],
              "properties": {
                "scope": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "kind",
                    "workspaceKey",
                    "projectKey"
                  ],
                  "properties": {
                    "kind": {
                      "const": "project"
                    },
                    "workspaceKey": {
                      "type": "string",
                      "minLength": 1
                    },
                    "projectKey": {
                      "type": "string",
                      "minLength": 1
                    }
                  }
                },
                "reportKey": {
                  "type": "string",
                  "minLength": 1
                },
                "revisionRef": {
                  "type": "string",
                  "pattern": "^sha256:[0-9a-f]{64}$"
                }
              }
            },
            "newRevisionRef": {
              "type": "string",
              "pattern": "^sha256:[0-9a-f]{64}$"
            },
            "newGeneration": {
              "type": "integer",
              "minimum": 0
            },
            "semanticDigest": {
              "type": "string",
              "pattern": "^sha256:[0-9a-f]{64}$"
            }
          }
        },
        {
          "type": "null"
        }
      ]
    },
    "diagnostics": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "code",
          "path",
          "severity",
          "retryable",
          "params"
        ],
        "properties": {
          "code": {
            "type": "string",
            "pattern": "^PX_"
          },
          "path": {
            "type": "string"
          },
          "severity": {
            "enum": [
              "error",
              "warning",
              "info"
            ]
          },
          "retryable": {
            "type": "boolean"
          },
          "params": {
            "type": "object"
          }
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "state": {
            "const": "pending"
          }
        },
        "required": [
          "state"
        ]
      },
      "then": {
        "properties": {
          "receipt": {
            "type": "null"
          },
          "diagnostics": {
            "type": "array",
            "maxItems": 0
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "state": {
            "const": "applied"
          }
        },
        "required": [
          "state"
        ]
      },
      "then": {
        "properties": {
          "receipt": {
            "type": "object"
          },
          "diagnostics": {
            "type": "array",
            "maxItems": 0
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "state": {
            "const": "refused"
          }
        },
        "required": [
          "state"
        ]
      },
      "then": {
        "properties": {
          "receipt": {
            "type": "null"
          },
          "diagnostics": {
            "type": "array",
            "minItems": 1
          }
        }
      }
    }
  ]
}
