{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://proofwork.example/schemas/commercial-kpi-framework-1.0.json",
  "title": "Commercial KPI Framework",
  "description": "Machine-readable KPI records and Composite Business Index inputs.",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "modules", "kpis", "cbiScoring"],
  "properties": {
    "schemaVersion": {
      "const": "1.0"
    },
    "reportingPeriod": {
      "$ref": "#/$defs/period"
    },
    "modules": {
      "type": "array",
      "minItems": 5,
      "uniqueItems": true,
      "items": {
        "type": "string",
        "enum": ["company", "team", "product", "process", "cost"]
      }
    },
    "kpis": {
      "type": "array",
      "minItems": 20,
      "items": {
        "$ref": "#/$defs/kpi"
      }
    },
    "cbiScoring": {
      "$ref": "#/$defs/cbiScoring"
    },
    "portfolioCbi": {
      "type": ["number", "null"],
      "minimum": 0,
      "maximum": 100
    }
  },
  "$defs": {
    "module": {
      "type": "string",
      "enum": ["company", "team", "product", "process", "cost"]
    },
    "direction": {
      "type": "string",
      "enum": ["higher", "lower", "band"]
    },
    "unit": {
      "type": "string",
      "enum": [
        "count",
        "percent",
        "USD",
        "months",
        "hours",
        "days",
        "rate",
        "score"
      ]
    },
    "cadence": {
      "type": "string",
      "enum": ["release", "weekly", "monthly", "quarterly"]
    },
    "period": {
      "type": "object",
      "additionalProperties": false,
      "required": ["start", "end"],
      "properties": {
        "start": {
          "type": "string",
          "format": "date"
        },
        "end": {
          "type": "string",
          "format": "date"
        }
      }
    },
    "target": {
      "oneOf": [
        {
          "type": "number"
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["min", "max"],
          "properties": {
            "min": {
              "type": "number"
            },
            "max": {
              "type": "number"
            }
          }
        }
      ]
    },
    "componentScore": {
      "type": ["number", "null"],
      "minimum": 0,
      "maximum": 100
    },
    "kpi": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "module",
        "name",
        "definition",
        "formula",
        "unit",
        "direction",
        "target",
        "cadence",
        "source",
        "period",
        "measuredAt",
        "value",
        "dataQuality",
        "cbi"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^(company|team|product|process|cost)\\.[a-z][a-z0-9_]*$"
        },
        "module": {
          "$ref": "#/$defs/module"
        },
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 120
        },
        "definition": {
          "type": "string",
          "minLength": 1
        },
        "formula": {
          "type": "string",
          "minLength": 1
        },
        "unit": {
          "$ref": "#/$defs/unit"
        },
        "currency": {
          "type": ["string", "null"],
          "pattern": "^[A-Z]{3}$"
        },
        "direction": {
          "$ref": "#/$defs/direction"
        },
        "target": {
          "$ref": "#/$defs/target"
        },
        "cadence": {
          "$ref": "#/$defs/cadence"
        },
        "source": {
          "type": "string",
          "minLength": 1,
          "maxLength": 300
        },
        "period": {
          "$ref": "#/$defs/period"
        },
        "measuredAt": {
          "type": "string",
          "format": "date-time"
        },
        "value": {
          "type": ["number", "null"]
        },
        "sampleSize": {
          "type": ["integer", "null"],
          "minimum": 0
        },
        "dataQuality": {
          "type": "object",
          "additionalProperties": false,
          "required": ["expectedObservations", "validObservations", "freshnessHours", "checksPassed"],
          "properties": {
            "expectedObservations": {
              "type": "integer",
              "minimum": 0
            },
            "validObservations": {
              "type": "integer",
              "minimum": 0
            },
            "freshnessHours": {
              "type": "number",
              "minimum": 0
            },
            "checksPassed": {
              "type": "boolean"
            }
          }
        },
        "caveat": {
          "type": ["string", "null"],
          "maxLength": 1000
        },
        "cbi": {
          "type": "object",
          "additionalProperties": false,
          "required": ["attainment", "businessImpact", "coverage", "integrity", "score"],
          "properties": {
            "attainment": {
              "$ref": "#/$defs/componentScore"
            },
            "businessImpact": {
              "$ref": "#/$defs/componentScore"
            },
            "coverage": {
              "$ref": "#/$defs/componentScore"
            },
            "integrity": {
              "$ref": "#/$defs/componentScore"
            },
            "score": {
              "$ref": "#/$defs/componentScore"
            },
            "status": {
              "type": "string",
              "enum": ["unscored", "review", "monitor", "on_track"]
            },
            "rationale": {
              "type": ["string", "null"],
              "maxLength": 1000
            }
          }
        }
      }
    },
    "cbiScoring": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "weights", "formula", "bands"],
      "properties": {
        "name": {
          "const": "Composite Business Index"
        },
        "weights": {
          "type": "object",
          "additionalProperties": false,
          "required": ["attainment", "businessImpact", "coverage", "integrity"],
          "properties": {
            "attainment": {
              "const": 0.5
            },
            "businessImpact": {
              "const": 0.2
            },
            "coverage": {
              "const": 0.15
            },
            "integrity": {
              "const": 0.15
            }
          }
        },
        "formula": {
          "type": "string",
          "const": "0.50*attainment + 0.20*businessImpact + 0.15*coverage + 0.15*integrity"
        },
        "bands": {
          "type": "object",
          "additionalProperties": false,
          "required": ["reviewBelow", "monitorBelow", "onTrackFrom"],
          "properties": {
            "reviewBelow": {
              "const": 60
            },
            "monitorBelow": {
              "const": 80
            },
            "onTrackFrom": {
              "const": 80
            }
          }
        }
      }
    }
  }
}
