{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://aiox.dev/schemas/quality-metrics.schema.json",
  "title": "Quality Metrics",
  "description": "Schema for quality gate metrics data",
  "type": "object",
  "required": ["version", "lastUpdated", "layers", "trends", "history"],
  "properties": {
    "version": {
      "type": "string",
      "const": "1.0",
      "description": "Schema version"
    },
    "lastUpdated": {
      "type": "string",
      "format": "date-time",
      "description": "Last time metrics were updated"
    },
    "retentionDays": {
      "type": "integer",
      "default": 30,
      "minimum": 1,
      "maximum": 365,
      "description": "Number of days to retain history"
    },
    "layers": {
      "type": "object",
      "description": "Aggregated metrics per layer",
      "properties": {
        "layer1": { "$ref": "#/definitions/layerMetrics" },
        "layer2": { "$ref": "#/definitions/layer2Metrics" },
        "layer3": { "$ref": "#/definitions/layerMetrics" }
      },
      "additionalProperties": false
    },
    "trends": {
      "type": "object",
      "description": "Trend data over time",
      "properties": {
        "autoCatchRate": {
          "type": "array",
          "items": { "$ref": "#/definitions/trendPoint" },
          "description": "Auto-catch rate trend (Layer 2 automated detection)"
        },
        "passRates": {
          "type": "array",
          "items": { "$ref": "#/definitions/trendPoint" },
          "description": "Overall pass rate trend"
        }
      },
      "additionalProperties": false
    },
    "history": {
      "type": "array",
      "items": { "$ref": "#/definitions/runRecord" },
      "description": "Historical run records"
    }
  },
  "definitions": {
    "layerMetrics": {
      "type": "object",
      "description": "Aggregated metrics for a layer",
      "properties": {
        "passRate": {
          "type": "number",
          "minimum": 0,
          "maximum": 1,
          "description": "Pass rate (0-1)"
        },
        "avgTimeMs": {
          "type": "integer",
          "minimum": 0,
          "description": "Average execution time in milliseconds"
        },
        "totalRuns": {
          "type": "integer",
          "minimum": 0,
          "description": "Total number of runs"
        },
        "lastRun": {
          "type": ["string", "null"],
          "description": "Timestamp of last run (null if never run)"
        }
      },
      "additionalProperties": false
    },
    "layer2Metrics": {
      "type": "object",
      "description": "Extended metrics for Layer 2 (PR automation)",
      "properties": {
        "passRate": {
          "type": "number",
          "minimum": 0,
          "maximum": 1,
          "description": "Pass rate (0-1)"
        },
        "avgTimeMs": {
          "type": "integer",
          "minimum": 0,
          "description": "Average execution time in milliseconds"
        },
        "totalRuns": {
          "type": "integer",
          "minimum": 0,
          "description": "Total number of runs"
        },
        "lastRun": {
          "type": ["string", "null"],
          "description": "Timestamp of last run (null if never run)"
        },
        "autoCatchRate": {
          "type": "number",
          "minimum": 0,
          "description": "Rate of issues caught automatically"
        },
        "coderabbit": {
          "type": "object",
          "description": "CodeRabbit specific metrics",
          "properties": {
            "active": {
              "type": "boolean",
              "description": "Whether CodeRabbit is active"
            },
            "findingsCount": {
              "type": "integer",
              "minimum": 0,
              "description": "Total findings count"
            },
            "severityBreakdown": {
              "type": "object",
              "properties": {
                "critical": { "type": "integer", "minimum": 0 },
                "high": { "type": "integer", "minimum": 0 },
                "medium": { "type": "integer", "minimum": 0 },
                "low": { "type": "integer", "minimum": 0 }
              },
              "additionalProperties": false
            }
          },
          "additionalProperties": false
        },
        "quinn": {
          "type": "object",
          "description": "Quinn (QA agent) specific metrics",
          "properties": {
            "findingsCount": {
              "type": "integer",
              "minimum": 0,
              "description": "Total findings count"
            },
            "topCategories": {
              "type": "array",
              "items": { "type": "string" },
              "description": "Top issue categories"
            }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "trendPoint": {
      "type": "object",
      "description": "A single point in a trend",
      "required": ["date", "value"],
      "properties": {
        "date": {
          "type": "string",
          "format": "date",
          "description": "Date of the data point (YYYY-MM-DD)"
        },
        "value": {
          "type": "number",
          "description": "Value at this point"
        }
      },
      "additionalProperties": false
    },
    "runRecord": {
      "type": "object",
      "description": "Record of a single run",
      "required": ["timestamp", "layer", "passed"],
      "properties": {
        "timestamp": {
          "type": "string",
          "format": "date-time",
          "description": "When the run occurred"
        },
        "layer": {
          "type": "integer",
          "enum": [1, 2, 3],
          "description": "Which layer was run"
        },
        "passed": {
          "type": "boolean",
          "description": "Whether the run passed"
        },
        "durationMs": {
          "type": "integer",
          "minimum": 0,
          "description": "Duration in milliseconds"
        },
        "findingsCount": {
          "type": "integer",
          "minimum": 0,
          "description": "Number of findings/issues"
        },
        "metadata": {
          "type": "object",
          "description": "Additional metadata for the run",
          "properties": {
            "storyId": { "type": "string" },
            "branchName": { "type": "string" },
            "commitHash": { "type": "string" },
            "triggeredBy": { "type": "string" },
            "severityBreakdown": {
              "type": "object",
              "properties": {
                "critical": { "type": "integer" },
                "high": { "type": "integer" },
                "medium": { "type": "integer" },
                "low": { "type": "integer" }
              }
            }
          },
          "additionalProperties": true
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
