{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://archsightlabs.github.io/aios/schemas/architecture-health/0.1/profile.schema.json",
  "title": "ArchSight AIOS Architecture Health Profile 0.1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "id", "version", "rules", "requiredEvidence", "budgets"],
  "properties": {
    "schemaVersion": { "const": "0.1" },
    "id": { "type": "string", "minLength": 1 },
    "version": { "type": "string", "minLength": 1 },
    "allowBootstrap": { "type": "boolean", "default": false },
    "rules": {
      "type": "array",
      "items": { "$ref": "#/$defs/rule" }
    },
    "dependencyPolicy": { "$ref": "#/$defs/dependencyPolicy" },
    "requiredEvidence": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "commit": { "$ref": "#/$defs/stringArray" },
        "weekly": { "$ref": "#/$defs/stringArray" },
        "milestone": { "$ref": "#/$defs/stringArray" }
      }
    },
    "evidencePolicy": { "$ref": "#/$defs/evidencePolicy" },
    "constraintIntegrity": { "$ref": "#/$defs/constraintIntegrity" },
    "budgets": {
      "type": "array",
      "items": { "$ref": "#/$defs/budget" }
    },
    "analyzers": {
      "type": "array",
      "items": { "$ref": "#/$defs/analyzer" }
    }
  },
  "$defs": {
    "mode": {
      "type": "string",
      "enum": ["commit", "weekly", "milestone"]
    },
    "modes": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/mode" }
    },
    "severity": {
      "type": "string",
      "enum": ["note", "P2", "P1", "P0"]
    },
    "stringArray": {
      "type": "array",
      "uniqueItems": true,
      "items": { "type": "string", "minLength": 1 }
    },
    "evidencePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["requireProvenance", "requireArtifactDigest"],
      "properties": {
        "requireProvenance": { "$ref": "#/$defs/modes" },
        "requireArtifactDigest": { "$ref": "#/$defs/modes" }
      }
    },
    "constraintIntegrity": {
      "type": "object",
      "additionalProperties": false,
      "required": ["modes", "approvalEvidenceId"],
      "properties": {
        "modes": { "$ref": "#/$defs/modes" },
        "approvalEvidenceId": { "type": "string", "minLength": 1 }
      }
    },
    "rule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "dimension", "metric", "operator", "threshold", "severity", "gate", "modes", "message"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "dimension": { "type": "string", "minLength": 1 },
        "metric": { "type": "string", "minLength": 1 },
        "operator": {
          "type": "string",
          "enum": ["greaterThan", "greaterThanOrEqual", "lessThan", "lessThanOrEqual", "equals"]
        },
        "threshold": { "type": ["number", "string", "boolean"] },
        "severity": { "$ref": "#/$defs/severity" },
        "gate": { "type": "boolean" },
        "modes": { "$ref": "#/$defs/modes" },
        "message": { "type": "string", "minLength": 1 }
      }
    },
    "cyclePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["severity", "gate", "modes"],
      "properties": {
        "severity": { "$ref": "#/$defs/severity" },
        "gate": { "type": "boolean" },
        "modes": { "$ref": "#/$defs/modes" }
      }
    },
    "forbiddenDirection": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "from", "to", "severity", "gate", "modes"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "from": { "type": "string", "minLength": 1 },
        "to": { "type": "string", "minLength": 1 },
        "severity": { "$ref": "#/$defs/severity" },
        "gate": { "type": "boolean" },
        "modes": { "$ref": "#/$defs/modes" }
      }
    },
    "dependencyPolicy": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "cycles": { "$ref": "#/$defs/cyclePolicy" },
        "forbiddenDirections": {
          "type": "array",
          "items": { "$ref": "#/$defs/forbiddenDirection" }
        }
      }
    },
    "budget": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "ruleId", "scope", "owner", "reason", "ceiling", "expiresAt"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "ruleId": { "type": "string", "minLength": 1 },
        "scope": { "type": "string", "minLength": 1 },
        "owner": { "type": "string", "minLength": 1 },
        "reason": { "type": "string", "minLength": 1 },
        "ceiling": { "type": "integer", "minimum": 1 },
        "expiresAt": { "type": "string", "format": "date-time" }
      }
    },
    "analyzer": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "command", "modes", "required"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "command": { "type": "string", "minLength": 1 },
        "args": {
          "type": "array",
          "items": { "type": "string" }
        },
        "cwd": { "type": "string", "minLength": 1 },
        "modes": { "$ref": "#/$defs/modes" },
        "required": { "type": "boolean" },
        "timeoutMs": { "type": "integer", "minimum": 1 }
      }
    }
  }
}
