{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://craftquantities.com/schemas/cli-output.v1.json",
  "title": "craftquantities CLI output, version 1",
  "description": "What `craftquantities <command> --json` prints. The top level is stable within schema_version 1: keys are only ever added. `data` is command-specific and `raw` (with --raw) is the engine's complete answer. Quantities only: no prices; nothing sized.",
  "oneOf": [{ "$ref": "#/$defs/success" }, { "$ref": "#/$defs/failure" }],
  "$defs": {
    "figure": {
      "type": ["object", "null"],
      "required": ["value", "unit", "printed"],
      "properties": {
        "value": { "type": ["number", "null"] },
        "unit": { "type": ["string", "null"] },
        "printed": { "type": ["string", "null"] }
      }
    },
    "common": {
      "type": "object",
      "required": ["$schema", "schema_version", "ok", "command", "cli_version", "engine_version"],
      "properties": {
        "$schema": { "const": "https://craftquantities.com/schemas/cli-output.v1.json" },
        "schema_version": { "const": 1 },
        "command": { "type": ["string", "null"] },
        "cli_version": { "type": "string", "pattern": "^\\d+\\.\\d+\\.\\d+" },
        "engine_version": { "type": "string", "pattern": "^\\d+\\.\\d+\\.\\d+" }
      }
    },
    "success": {
      "allOf": [{ "$ref": "#/$defs/common" }],
      "type": "object",
      "required": ["ok", "title", "result", "breakdown", "inputs", "units", "sources", "warnings", "interactive_url", "interactive_label", "page_url", "citation", "licence", "data"],
      "properties": {
        "ok": { "const": true },
        "command": { "enum": ["calc", "takeoff", "search", "info", "convert", "tape", "sketch", "quote-check", "scope", "share", "init", "completion", "update"] },
        "title": { "type": "string" },
        "result": {
          "description": "The headline figure, in both systems where it has two; null when the command has no single figure (search, info, share).",
          "type": ["object", "null"],
          "required": ["label", "metric", "imperial"],
          "properties": {
            "label": { "type": ["string", "null"] },
            "metric": { "$ref": "#/$defs/figure" },
            "imperial": { "$ref": "#/$defs/figure" },
            "primary": { "enum": ["metric", "imperial"] },
            "note": { "type": "string" }
          }
        },
        "breakdown": { "type": "array", "items": { "type": "object" }, "description": "The lines: a calculator's breakdown, a takeoff's bill rows (section core | accessory | add-on), search results, inputs, quote lines." },
        "inputs": { "type": "array", "items": { "type": "object", "required": ["id"], "properties": { "id": { "type": "string" }, "label": { "type": ["string", "null"] }, "source": { "type": ["string", "null"] } } } },
        "units": {
          "type": "object",
          "required": ["system", "market"],
          "properties": { "system": { "enum": ["metric", "imperial", null] }, "market": { "enum": ["US", "UK", "CA", "AU", "INTL", null] } }
        },
        "sources": { "type": "array", "items": { "type": "object", "required": ["label"], "properties": { "label": { "type": "string" }, "url": { "type": "string", "format": "uri" }, "references": { "type": "array", "items": { "type": "string" } } } } },
        "warnings": { "type": "array", "items": { "type": "string" }, "description": "What a professional must decide (the engine's own refusals), and what the CLI assumed or noticed about a value (\"Note: …\")." },
        "interactive_url": {
          "description": "The craftquantities.com page with the figures filled in: see the drawing, edit visually, export. Present on every command that has a page.",
          "type": ["string", "null"],
          "pattern": "^https://craftquantities\\.com/"
        },
        "interactive_label": { "type": ["string", "null"] },
        "page_url": { "type": ["string", "null"] },
        "citation": { "type": ["string", "null"] },
        "licence": { "type": "object", "required": ["name", "url", "attribution"] },
        "data": { "type": "object" },
        "raw": {}
      }
    },
    "failure": {
      "allOf": [{ "$ref": "#/$defs/common" }],
      "type": "object",
      "required": ["ok", "error"],
      "properties": {
        "ok": { "const": false },
        "error": {
          "type": "object",
          "required": ["kind", "message", "exit_code", "suggestions"],
          "properties": {
            "kind": { "enum": ["usage", "not-found", "refused", "engine", "io", "network", "internal"] },
            "message": { "type": "string" },
            "exit_code": { "enum": [1, 2, 3, 4, 5] },
            "suggestions": { "type": "array", "items": { "type": "string" } },
            "hint": { "type": "string" },
            "engine_status": { "type": "integer" }
          }
        }
      }
    }
  }
}
