{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://developer.reits.tech/schemas/release-unit.v1.schema.json",
  "title": "REITs Release Unit",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "releaseId", "repository", "service", "channel", "environment", "source", "artifact", "sbom", "configuration", "migration", "secrets", "deployment", "rollback", "provenance"],
  "properties": {
    "schemaVersion": { "const": "developer.reits.tech/release-unit/v1" },
    "releaseId": { "type": "string", "minLength": 8 },
    "repository": { "type": "string", "pattern": "^[a-z0-9._-]+/[a-z0-9._-]+$" },
    "service": { "type": "string", "minLength": 2 },
    "channel": { "enum": ["systemd", "container", "sites", "native", "package"] },
    "environment": { "enum": ["production", "staging", "preview"] },
    "source": { "$ref": "#/$defs/source" },
    "artifact": { "$ref": "#/$defs/evidence" },
    "sbom": { "$ref": "#/$defs/evidence" },
    "configuration": { "$ref": "#/$defs/evidence" },
    "migration": { "$ref": "#/$defs/evidence" },
    "secrets": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "referencesOnly", "refs"],
      "properties": {
        "status": { "$ref": "#/$defs/status" },
        "referencesOnly": { "const": true },
        "refs": { "type": "array", "items": { "type": "string", "pattern": "^[A-Z][A-Z0-9_./:-]+$" }, "uniqueItems": true }
      }
    },
    "deployment": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "target", "observedAt"],
      "properties": {
        "status": { "$ref": "#/$defs/status" },
        "target": { "type": "string", "minLength": 2 },
        "observedAt": { "type": "string", "format": "date-time" },
        "runtimeIdentityUrl": { "type": ["string", "null"], "format": "uri" }
      }
    },
    "rollback": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "targetReleaseId", "verifiedAt"],
      "properties": {
        "status": { "$ref": "#/$defs/status" },
        "targetReleaseId": { "type": ["string", "null"] },
        "verifiedAt": { "type": ["string", "null"], "format": "date-time" }
      }
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "predicateType", "builderId", "signed"],
      "properties": {
        "status": { "$ref": "#/$defs/status" },
        "predicateType": { "type": ["string", "null"], "format": "uri" },
        "builderId": { "type": ["string", "null"] },
        "signed": { "type": "boolean" }
      }
    }
  },
  "$defs": {
    "status": { "enum": ["BOUND", "OBSERVED", "PARTIAL", "MISSING", "BLOCKED"] },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "revision", "ref", "dirty"],
      "properties": {
        "status": { "$ref": "#/$defs/status" },
        "revision": { "type": "string", "pattern": "^[0-9a-f]{40}$" },
        "ref": { "type": "string", "minLength": 1 },
        "dirty": { "const": false }
      }
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "kind", "digest", "path"],
      "properties": {
        "status": { "$ref": "#/$defs/status" },
        "kind": { "type": "string", "minLength": 2 },
        "digest": { "type": ["string", "null"], "pattern": "^sha256:[0-9a-f]{64}$" },
        "path": { "type": ["string", "null"] }
      }
    }
  }
}
