{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "kicad_cruncher.design_review_manifest.d0",
  "title": "KiCad Cruncher Design Review Manifest (D0)",
  "description": "Agent-first D0 design review artifact index aligned with Altium Cruncher D0.",
  "type": "object",
  "required": [
    "schema",
    "status",
    "input",
    "root_kind",
    "files",
    "designs",
    "issues"
  ],
  "properties": {
    "schema": {
      "const": "kicad_cruncher.design_review_manifest.d0"
    },
    "status": {
      "type": "string",
      "enum": ["complete", "partial", "incomplete"]
    },
    "input": {
      "type": "string",
      "minLength": 1
    },
    "root_kind": {
      "const": "design"
    },
    "files": {
      "type": "object",
      "required": [
        "readme",
        "review_summary",
        "compilation",
        "report"
      ],
      "properties": {
        "readme": { "$ref": "#/$defs/artifact_path" },
        "review_summary": { "$ref": "#/$defs/artifact_path" },
        "compilation": { "$ref": "#/$defs/artifact_path" },
        "report": { "$ref": "#/$defs/artifact_path" }
      },
      "additionalProperties": false
    },
    "designs": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/design_entry" }
    },
    "issues": {
      "type": "array",
      "items": { "type": "string" }
    },
    "design_facts": { "$ref": "#/$defs/design_facts" }
  },
  "additionalProperties": false,
  "$defs": {
    "artifact_path": {
      "type": "string",
      "minLength": 1,
      "pattern": "^(?![A-Za-z]:)(?!/)(?!.*\\\\)(?!.*(?:^|/)\\.\\.?(?:/|$))[^/]+(?:/[^/]+)*$",
      "description": "Forward-slash path relative to the bundle root."
    },
    "lower_sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "design_entry": {
      "type": "object",
      "required": [
        "definition_id",
        "prefix",
        "files",
        "sheets",
        "boards"
      ],
      "properties": {
        "definition_id": { "type": "string", "minLength": 1 },
        "prefix": { "type": "string" },
        "files": {
          "type": "object",
          "required": ["bom", "notes"],
          "properties": {
            "bom": { "$ref": "#/$defs/artifact_path" },
            "notes": { "$ref": "#/$defs/artifact_path" }
          },
          "additionalProperties": false
        },
        "sheets": {
          "type": "array",
          "items": { "$ref": "#/$defs/sheet_entry" }
        },
        "boards": {
          "type": "array",
          "items": { "$ref": "#/$defs/board_entry" }
        },
        "source_documents": {
          "type": "array",
          "items": { "$ref": "#/$defs/source_document_entry" }
        }
      },
      "additionalProperties": false
    },
    "sheet_entry": {
      "type": "object",
      "required": [
        "sheet",
        "sheet_name",
        "sheet_path",
        "sheet_number",
        "sheet_count",
        "page_ref",
        "status"
      ],
      "properties": {
        "sheet": { "type": "string", "minLength": 1 },
        "sheet_name": { "type": "string" },
        "sheet_path": { "type": "string", "minLength": 1 },
        "sheet_number": { "type": "integer", "minimum": 1 },
        "sheet_count": { "type": "integer", "minimum": 1 },
        "page_ref": { "type": "string", "minLength": 1 },
        "status": { "type": "string", "enum": ["written", "unavailable"] },
        "svg": { "$ref": "#/$defs/artifact_path" },
        "layout": { "$ref": "#/$defs/artifact_path" },
        "reason": { "type": "string" }
      },
      "additionalProperties": false
    },
    "board_layer_entry": {
      "type": "object",
      "required": [
        "layer",
        "file",
        "included_layers",
        "drill_slot_record_count"
      ],
      "properties": {
        "layer": { "type": "string", "minLength": 1 },
        "file": { "$ref": "#/$defs/artifact_path" },
        "included_layers": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        },
        "drill_slot_record_count": { "type": "integer", "minimum": 0 }
      },
      "additionalProperties": false
    },
    "board_entry": {
      "type": "object",
      "required": [
        "board",
        "source_file",
        "status"
      ],
      "properties": {
        "board": { "type": "string", "minLength": 1 },
        "source_file": { "type": "string", "minLength": 1 },
        "status": { "type": "string", "enum": ["written", "unavailable"] },
        "pnp": { "$ref": "#/$defs/artifact_path" },
        "board_summary": { "$ref": "#/$defs/artifact_path" },
        "routing_context": { "$ref": "#/$defs/artifact_path" },
        "svg_manifest": { "$ref": "#/$defs/artifact_path" },
        "layers": {
          "type": "array",
          "items": { "$ref": "#/$defs/board_layer_entry" }
        },
        "views": {
          "type": "array",
          "items": { "type": "object" }
        },
        "reason": { "type": "string" }
      },
      "additionalProperties": false
    },
    "source_document_entry": {
      "type": "object",
      "required": ["kind", "file"],
      "properties": {
        "kind": { "type": "string", "minLength": 1 },
        "file": { "$ref": "#/$defs/artifact_path" }
      },
      "additionalProperties": false
    },
    "design_facts": {
      "type": "object",
      "required": [
        "backend",
        "engine_version",
        "source_snapshot_sha256",
        "compilation_sha256"
      ],
      "properties": {
        "backend": { "type": "string", "minLength": 1 },
        "engine_version": { "type": "string", "minLength": 1 },
        "source_snapshot_sha256": { "$ref": "#/$defs/lower_sha256" },
        "compilation_sha256": { "$ref": "#/$defs/lower_sha256" }
      },
      "additionalProperties": false
    }
  }
}
