{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.driftframe.org/site-reading-governance/v2.0/promotion-receipt.schema.json",
  "title": "Repository Promotion Receipt",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "promotion_receipt_id",
    "repository",
    "target_release",
    "promotion_state",
    "evidence_mode",
    "assurance_stage",
    "release_coordinate",
    "required_evidence",
    "accountable_role",
    "metadata"
  ],
  "properties": {
    "promotion_receipt_id": {
      "$ref": "common.schema.json#/$defs/identifier"
    },
    "repository": {
      "type": "string",
      "pattern": "^[a-z0-9]+/[a-z0-9][a-z0-9-]*$",
      "minLength": 3,
      "maxLength": 200
    },
    "target_release": {
      "type": "string",
      "pattern": "^governance-v(?:0|[1-9][0-9]*)\\.(?:0|[1-9][0-9]*)\\.(?:0|[1-9][0-9]*)$"
    },
    "promotion_state": {
      "type": "string",
      "enum": [
        "candidate_ready",
        "release_observed"
      ]
    },
    "evidence_mode": {
      "$ref": "common.schema.json#/$defs/evidenceMode"
    },
    "assurance_stage": {
      "$ref": "common.schema.json#/$defs/assuranceStage"
    },
    "release_coordinate": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "git_object_format",
            "tag",
            "tag_object_oid",
            "commit_oid",
            "archive_format",
            "archive_digest",
            "promoted_at",
            "local_proof_receipt",
            "hosted_check_contexts",
            "hosted_automation_receipt",
            "ruleset_receipt",
            "codeowner_resolution",
            "tag_signature_receipt"
          ],
          "properties": {
            "git_object_format": {
              "type": "string",
              "enum": [
                "sha1",
                "sha256"
              ]
            },
            "tag": {
              "type": "string",
              "pattern": "^governance-v(?:0|[1-9][0-9]*)\\.(?:0|[1-9][0-9]*)\\.(?:0|[1-9][0-9]*)$"
            },
            "tag_object_oid": {
              "type": "string",
              "pattern": "^(?:(?!0{40}$)[a-f0-9]{40}|(?!0{64}$)[a-f0-9]{64})$"
            },
            "commit_oid": {
              "type": "string",
              "pattern": "^(?:(?!0{40}$)[a-f0-9]{40}|(?!0{64}$)[a-f0-9]{64})$"
            },
            "archive_format": {
              "type": "string",
              "const": "git-tar"
            },
            "archive_digest": {
              "type": "string",
              "pattern": "^sha256:(?!0{64}$)[a-f0-9]{64}$"
            },
            "promoted_at": {
              "type": "string",
              "format": "date-time"
            },
            "local_proof_receipt": {
              "$ref": "common.schema.json#/$defs/governedLocator"
            },
            "hosted_check_contexts": {
              "type": "array",
              "uniqueItems": true,
              "items": {
                "type": "string",
                "minLength": 1,
                "maxLength": 200
              }
            },
            "hosted_automation_receipt": {
              "oneOf": [
                {
                  "type": "null"
                },
                {
                  "$ref": "common.schema.json#/$defs/governedLocator"
                }
              ]
            },
            "ruleset_receipt": {
              "$ref": "common.schema.json#/$defs/governedLocator"
            },
            "codeowner_resolution": {
              "$ref": "common.schema.json#/$defs/governedLocator"
            },
            "tag_signature_receipt": {
              "$ref": "common.schema.json#/$defs/governedLocator"
            }
          },
          "allOf": [
            {
              "if": {
                "properties": {
                  "git_object_format": {
                    "const": "sha1"
                  }
                }
              },
              "then": {
                "properties": {
                  "tag_object_oid": {
                    "pattern": "^(?!0{40}$)[a-f0-9]{40}$"
                  },
                  "commit_oid": {
                    "pattern": "^(?!0{40}$)[a-f0-9]{40}$"
                  }
                }
              },
              "else": {
                "properties": {
                  "tag_object_oid": {
                    "pattern": "^(?!0{64}$)[a-f0-9]{64}$"
                  },
                  "commit_oid": {
                    "pattern": "^(?!0{64}$)[a-f0-9]{64}$"
                  }
                }
              }
            },
            {
              "if": {
                "properties": {
                  "hosted_check_contexts": {
                    "minItems": 1
                  }
                }
              },
              "then": {
                "properties": {
                  "hosted_automation_receipt": {
                    "type": "string"
                  }
                }
              },
              "else": {
                "properties": {
                  "hosted_automation_receipt": {
                    "type": "null"
                  }
                }
              }
            }
          ]
        }
      ]
    },
    "required_evidence": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": {
        "type": "string",
        "minLength": 10,
        "maxLength": 500
      }
    },
    "accountable_role": {
      "type": "string",
      "minLength": 3,
      "maxLength": 120
    },
    "metadata": {
      "$ref": "common.schema.json#/$defs/recordMetadata"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "promotion_state": {
            "const": "candidate_ready"
          }
        }
      },
      "then": {
        "properties": {
          "evidence_mode": {
            "const": "planned"
          },
          "assurance_stage": {
            "const": "specified"
          },
          "release_coordinate": {
            "type": "null"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "promotion_state": {
            "const": "release_observed"
          }
        }
      },
      "then": {
        "properties": {
          "evidence_mode": {
            "const": "observed"
          },
          "assurance_stage": {
            "enum": [
              "verified",
              "qualified",
              "validated",
              "accepted"
            ]
          },
          "release_coordinate": {
            "type": "object"
          }
        }
      }
    }
  ]
}
