{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/observability/observability-signal.v1.schema.json",
  "title": "RootBlocks Observability Signal v1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "signalId", "standard", "emittedAt", "window", "source", "subject", "category", "severity", "confidence", "evidenceRefs"],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.observability-signal/v1"
    },
    "signalId": {
      "$ref": "#/$defs/id"
    },
    "standard": {
      "$ref": "#/$defs/standardRef"
    },
    "emittedAt": {
      "type": "string",
      "format": "date-time"
    },
    "window": {
      "$ref": "#/$defs/window"
    },
    "source": {
      "$ref": "#/$defs/source"
    },
    "subject": {
      "$ref": "#/$defs/subject"
    },
    "category": {
      "enum": [
        "availability",
        "performance",
        "reliability",
        "data-health",
        "cache-health",
        "search-health",
        "queue-health",
        "infrastructure-health",
        "release-health",
        "business-health",
        "cost-health",
        "complexity-health",
        "risk-health"
      ]
    },
    "signalType": {
      "enum": ["metric", "log-derived", "trace-derived", "health-check", "business-event", "cost", "risk", "complexity"]
    },
    "metric": {
      "$ref": "#/$defs/metric"
    },
    "eventSummary": {
      "type": "string",
      "minLength": 1,
      "maxLength": 500
    },
    "severity": {
      "enum": ["info", "warning", "degraded", "critical"]
    },
    "confidence": {
      "type": "number",
      "minimum": 0,
      "maximum": 1
    },
    "dimensions": {
      "type": "object",
      "additionalProperties": {
        "type": "string",
        "minLength": 1,
        "maxLength": 160
      }
    },
    "knownGaps": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 240
      }
    },
    "evidenceRefs": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/path"
      },
      "uniqueItems": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "signalType": {
            "enum": ["metric", "cost"]
          }
        },
        "required": ["signalType"]
      },
      "then": {
        "required": ["metric"]
      }
    },
    {
      "if": {
        "properties": {
          "signalType": {
            "enum": ["business-event", "log-derived", "trace-derived", "health-check", "risk", "complexity"]
          }
        },
        "required": ["signalType"]
      },
      "then": {
        "required": ["eventSummary"]
      }
    }
  ],
  "$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_./@-]+$"
    },
    "standardRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "version", "hash"],
      "properties": {
        "bundleId": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "window": {
      "type": "object",
      "additionalProperties": false,
      "required": ["from", "to", "aggregation"],
      "properties": {
        "from": {
          "type": "string",
          "format": "date-time"
        },
        "to": {
          "type": "string",
          "format": "date-time"
        },
        "aggregation": {
          "enum": ["point", "sum", "avg", "min", "max", "p50", "p95", "p99", "count", "rate"]
        }
      }
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["monitorId", "adapter", "telemetryBackend"],
      "properties": {
        "monitorId": {
          "$ref": "#/$defs/id"
        },
        "adapter": {
          "enum": ["opentelemetry", "application", "database", "redis", "search", "queue", "cloud", "cost", "manual", "custom"]
        },
        "telemetryBackend": {
          "type": "string",
          "minLength": 1,
          "maxLength": 120
        }
      }
    },
    "subject": {
      "type": "object",
      "additionalProperties": false,
      "required": ["ownershipKnown"],
      "properties": {
        "ownershipKnown": {
          "type": "boolean"
        },
        "domainId": {
          "$ref": "#/$defs/id"
        },
        "boundedContextId": {
          "$ref": "#/$defs/id"
        },
        "workflowId": {
          "$ref": "#/$defs/id"
        },
        "componentId": {
          "$ref": "#/$defs/id"
        },
        "dataResourceId": {
          "$ref": "#/$defs/id"
        },
        "infrastructureResourceId": {
          "$ref": "#/$defs/id"
        },
        "integrationId": {
          "$ref": "#/$defs/id"
        },
        "governanceIssue": {
          "type": "string",
          "minLength": 1,
          "maxLength": 240
        }
      }
    },
    "metric": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "value", "unit"],
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9._-]{2,160}$"
        },
        "value": {
          "type": "number"
        },
        "unit": {
          "enum": ["ms", "count", "percent", "ratio", "bytes", "seconds", "requests-per-second", "currency-usd", "custom"]
        },
        "threshold": {
          "type": "number"
        }
      }
    }
  }
}
