{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/release/release-evidence.v1.schema.json",
  "title": "RootBlocks Release Evidence v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "evidenceId",
    "candidateId",
    "releaseVersion",
    "bundle",
    "state",
    "result",
    "generatedAt",
    "releaseImpact",
    "gates",
    "approvals",
    "artifacts",
    "signature",
    "publication"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.release-evidence/v1"
    },
    "evidenceId": {
      "type": "string",
      "pattern": "^release-evidence-[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "candidateId": {
      "type": "string",
      "pattern": "^release-candidate-[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "releaseVersion": {
      "$ref": "#/$defs/semver"
    },
    "bundle": {
      "$ref": "#/$defs/bundleRef"
    },
    "state": {
      "enum": ["validated", "approved", "signed", "published", "deprecated", "revoked"]
    },
    "result": {
      "enum": ["pass", "block", "revoked"]
    },
    "generatedAt": {
      "type": "string",
      "format": "date-time"
    },
    "releaseImpact": {
      "type": "array",
      "minItems": 1,
      "items": {
        "enum": ["breaking", "security", "compatibility", "schema", "rule", "golden", "tooling", "documentation", "component", "distribution", "release", "audit", "architecture-profile", "compiler-instruction", "structured-source", "source-audit", "buildpack-project"]
      },
      "uniqueItems": true
    },
    "gates": {
      "type": "array",
      "minItems": 6,
      "items": {
        "$ref": "#/$defs/gateResult"
      },
      "contains": {
        "properties": {
          "name": {
            "const": "validator"
          },
          "status": {
            "const": "passed"
          }
        },
        "required": ["name", "status"]
      }
    },
    "approvals": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/approval"
      }
    },
    "artifacts": {
      "$ref": "#/$defs/artifacts"
    },
    "signature": {
      "$ref": "#/$defs/signature"
    },
    "publication": {
      "$ref": "#/$defs/publication"
    },
    "blockingReasons": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      }
    },
    "revocation": {
      "$ref": "#/$defs/revocation"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "result": {
            "const": "pass"
          }
        },
        "required": ["result"]
      },
      "then": {
        "properties": {
          "gates": {
            "not": {
              "contains": {
                "properties": {
                  "status": {
                    "const": "failed"
                  },
                  "severity": {
                    "const": "critical"
                  }
                },
                "required": ["status", "severity"]
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "state": {
            "enum": ["signed", "published"]
          }
        },
        "required": ["state"]
      },
      "then": {
        "properties": {
          "signature": {
            "properties": {
              "mode": {
                "const": "signed"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "state": {
            "const": "published"
          }
        },
        "required": ["state"]
      },
      "then": {
        "properties": {
          "publication": {
            "properties": {
              "status": {
                "const": "published"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "releaseImpact": {
            "contains": {
              "enum": ["breaking", "security", "compatibility"]
            }
          }
        },
        "required": ["releaseImpact"]
      },
      "then": {
        "properties": {
          "approvals": {
            "minItems": 1
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "result": {
            "const": "block"
          }
        },
        "required": ["result"]
      },
      "then": {
        "required": ["blockingReasons"],
        "properties": {
          "blockingReasons": {
            "minItems": 1
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "state": {
            "const": "revoked"
          }
        },
        "required": ["state"]
      },
      "then": {
        "required": ["revocation"],
        "properties": {
          "result": {
            "const": "revoked"
          },
          "publication": {
            "properties": {
              "status": {
                "const": "revoked"
              }
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[A-Za-z0-9.-]+)?(\\+[A-Za-z0-9.-]+)?$"
    },
    "hash": {
      "type": "string",
      "pattern": "^[a-f0-9]{64,128}$"
    },
    "path": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_./@-]+$"
    },
    "bundleRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "bundleKind", "version", "manifestHash"],
      "properties": {
        "bundleId": {
          "$ref": "#/$defs/id"
        },
        "bundleKind": {
          "enum": [
            "rootblocks-global-standard",
            "organization-component-extension",
            "project-component-extension",
            "personal-component-extension"
          ]
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "gateName": {
      "enum": [
        "validator",
        "golden",
        "compatibility",
        "security",
        "docs",
        "packager",
        "signer",
        "distribution",
        "architecture-profile",
        "source-audit",
        "construction-language",
        "semantic-engine",
        "agent-codegen",
        "component-extension",
        "approval"
      ]
    },
    "gateResult": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "status", "severity", "evidencePath"],
      "properties": {
        "name": {
          "$ref": "#/$defs/gateName"
        },
        "status": {
          "enum": ["passed", "failed", "skipped", "not-applicable"]
        },
        "severity": {
          "enum": ["critical", "advisory"]
        },
        "evidencePath": {
          "$ref": "#/$defs/path"
        },
        "summary": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "approval": {
      "type": "object",
      "additionalProperties": false,
      "required": ["role", "approverId", "approvedAt", "decision"],
      "properties": {
        "role": {
          "enum": ["standard-owner", "security-reviewer", "compatibility-reviewer", "architecture-reviewer", "release-manager"]
        },
        "approverId": {
          "type": "string",
          "pattern": "^[a-zA-Z0-9][a-zA-Z0-9_.:@-]{1,127}$"
        },
        "approvedAt": {
          "type": "string",
          "format": "date-time"
        },
        "decision": {
          "enum": ["approved", "rejected"]
        },
        "comment": {
          "type": "string",
          "maxLength": 500
        }
      }
    },
    "artifacts": {
      "type": "object",
      "additionalProperties": false,
      "required": ["manifestPath", "manifestHash", "bundleArchivePath", "bundleHash", "releaseNotesPath"],
      "properties": {
        "manifestPath": {
          "$ref": "#/$defs/path"
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        },
        "bundleArchivePath": {
          "$ref": "#/$defs/path"
        },
        "bundleHash": {
          "$ref": "#/$defs/hash"
        },
        "releaseNotesPath": {
          "$ref": "#/$defs/path"
        },
        "distributionIndexPath": {
          "$ref": "#/$defs/path"
        },
        "sourceAuditReportPath": {
          "$ref": "#/$defs/path"
        },
        "sourceAuditReportHash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "signature": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "algorithm", "keyId", "signatureRef"],
      "properties": {
        "mode": {
          "enum": ["unsigned-dev-only", "signed"]
        },
        "algorithm": {
          "enum": ["none", "ed25519", "rsa-pss-sha256"]
        },
        "keyId": {
          "type": "string",
          "minLength": 1
        },
        "signatureRef": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "publication": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "distributionId"],
      "properties": {
        "status": {
          "enum": ["not-published", "published", "failed", "revoked"]
        },
        "distributionId": {
          "$ref": "#/$defs/id"
        },
        "publishedAt": {
          "type": "string",
          "format": "date-time"
        },
        "downloadManifestPath": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "revocation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["revokedAt", "reasonCode", "replacementRequired"],
      "properties": {
        "revokedAt": {
          "type": "string",
          "format": "date-time"
        },
        "reasonCode": {
          "enum": ["SIGNATURE_COMPROMISE", "SECURITY_DEFECT", "COMPATIBILITY_DEFECT", "BROKEN_ARTIFACT", "POLICY_VIOLATION"]
        },
        "replacementRequired": {
          "type": "boolean"
        },
        "replacementVersion": {
          "$ref": "#/$defs/semver"
        }
      }
    }
  }
}
