{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://haroya01.github.io/query-audit/schema/report-1.0.schema.json",
  "title": "QueryAudit report.json schema 1.0",
  "description": "Schema for the legacy 1.0 report.json envelope written by QueryAudit 0.5.x.",
  "type": "object",
  "required": ["schemaVersion", "reports"],
  "properties": {
    "schemaVersion": {
      "type": "string",
      "pattern": "^1\\.0\\.\\d+$"
    },
    "reports": {
      "type": "array",
      "items": { "$ref": "#/definitions/testReport" }
    }
  },
  "definitions": {
    "testReport": {
      "type": "object",
      "required": [
        "testClass",
        "testName",
        "summary",
        "confirmedIssues",
        "infoIssues",
        "acknowledgedIssues",
        "indexMetadata",
        "queries"
      ],
      "properties": {
        "testClass": { "type": ["string", "null"] },
        "testName": { "type": "string" },
        "summary": {
          "type": "object",
          "required": [
            "confirmedIssues",
            "infoIssues",
            "acknowledgedIssues",
            "uniquePatterns",
            "totalQueries",
            "executionTimeMs"
          ],
          "properties": {
            "confirmedIssues": { "type": "integer" },
            "infoIssues": { "type": "integer" },
            "acknowledgedIssues": { "type": "integer" },
            "uniquePatterns": { "type": "integer" },
            "totalQueries": { "type": "integer" },
            "executionTimeMs": { "type": "integer" }
          }
        },
        "confirmedIssues": { "$ref": "#/definitions/issueArray" },
        "infoIssues": { "$ref": "#/definitions/issueArray" },
        "acknowledgedIssues": { "$ref": "#/definitions/issueArray" },
        "indexMetadata": {
          "description": "Index state of every table referenced by a finding, or null when no metadata was collected (non-database test). Keys are table names.",
          "type": ["object", "null"],
          "additionalProperties": {
            "type": "array",
            "items": { "$ref": "#/definitions/index" }
          }
        },
        "queries": {
          "type": "array",
          "items": { "$ref": "#/definitions/query" }
        }
      }
    },
    "issueArray": {
      "type": "array",
      "items": { "$ref": "#/definitions/issue" }
    },
    "issue": {
      "type": "object",
      "required": ["type", "severity", "query", "table", "column", "detail", "suggestion", "sourceLocation"],
      "properties": {
        "type": { "type": "string", "description": "Issue code, e.g. n-plus-one, missing-where-index." },
        "severity": { "type": "string", "enum": ["ERROR", "WARNING", "INFO"] },
        "query": { "type": ["string", "null"] },
        "table": { "type": ["string", "null"] },
        "column": { "type": ["string", "null"] },
        "detail": { "type": ["string", "null"] },
        "suggestion": { "type": ["string", "null"] },
        "sourceLocation": {
          "type": ["string", "null"],
          "description": "Innermost application stack frame of the offending query."
        },
        "remediation": {
          "description": "Structured fix hint, present only for high-precision rules.",
          "type": "object",
          "required": ["kind"],
          "properties": {
            "kind": {
              "type": "string",
              "enum": [
                "add-index",
                "batch-fetch",
                "batch-insert",
                "batch-update",
                "add-where-clause",
                "add-limit",
                "select-explicit-columns"
              ]
            },
            "table": { "type": "string" },
            "columns": { "type": "array", "items": { "type": "string" } }
          }
        }
      }
    },
    "index": {
      "type": "object",
      "required": ["name", "unique", "columns", "cardinality"],
      "properties": {
        "name": { "type": "string" },
        "unique": { "type": "boolean" },
        "columns": {
          "type": "array",
          "items": { "type": "string" },
          "description": "Column names in index order."
        },
        "cardinality": {
          "type": "integer",
          "description": "Whole-index cardinality (SHOW INDEX semantics: the last column entry's cardinality)."
        }
      }
    },
    "query": {
      "type": "object",
      "required": ["sql", "normalizedSql", "executionTimeNanos", "stackTrace"],
      "properties": {
        "sql": { "type": "string" },
        "normalizedSql": { "type": ["string", "null"] },
        "executionTimeNanos": { "type": "integer" },
        "stackTrace": { "type": ["string", "null"] }
      }
    }
  }
}
