{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://openlec.org/schema/0.2/openlec.schema.json",
  "title": "open-lec Loss Exceedance Curve document",
  "type": "object",
  "required": ["openlec", "metadata", "scenarios"],
  "properties": {
    "openlec": { "type": "string", "pattern": "^0\\.[0-9]+$" },
    "metadata": { "$ref": "#/$defs/metadata" },
    "scenarios": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/scenario" }
    },
    "referenceLines": {
      "type": "array",
      "items": { "$ref": "#/$defs/referenceLine" }
    }
  },
  "$defs": {
    "metadata": {
      "type": "object",
      "required": ["currency"],
      "properties": {
        "currency": { "type": "string", "pattern": "^[A-Z]{3}$" },
        "title": { "type": "string" },
        "created": { "type": "string", "format": "date-time" },
        "horizon": {
          "type": "object",
          "required": ["value", "unit"],
          "properties": {
            "value": { "type": "number", "exclusiveMinimum": 0 },
            "unit": { "enum": ["year", "quarter", "month"] }
          }
        },
        "generator": {
          "type": "object",
          "required": ["name"],
          "properties": {
            "name": { "type": "string", "minLength": 1 },
            "version": { "type": "string" }
          }
        },
        "methodology": { "type": "string" },
        "iterations": { "type": "integer", "minimum": 1 }
      }
    },
    "scenario": {
      "type": "object",
      "required": ["id", "curve"],
      "properties": {
        "id": { "type": "string", "pattern": "^[A-Za-z0-9_-]+$" },
        "label": { "type": "string" },
        "role": { "enum": ["baseline", "treatment", "comparison"] },
        "curve": {
          "type": "object",
          "required": ["points"],
          "properties": {
            "points": {
              "type": "array",
              "minItems": 2,
              "items": { "$ref": "#/$defs/curvePoint" }
            },
            "interpolation": { "enum": ["linear", "log-loss"] }
          }
        },
        "bands": {
          "type": "array",
          "items": { "$ref": "#/$defs/band" }
        },
        "statistics": { "$ref": "#/$defs/statistics" },
        "annotations": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["loss", "label"],
            "properties": {
              "loss": { "type": "number", "minimum": 0 },
              "label": { "type": "string", "minLength": 1 }
            }
          }
        },
        "color": { "type": "string", "pattern": "^#[0-9a-fA-F]{6}$" }
      }
    },
    "curvePoint": {
      "type": "object",
      "required": ["loss", "p"],
      "properties": {
        "loss": { "type": "number", "minimum": 0 },
        "p": { "type": "number", "minimum": 0, "maximum": 1 }
      }
    },
    "band": {
      "type": "object",
      "required": ["level", "points"],
      "properties": {
        "level": { "type": "number", "exclusiveMinimum": 0, "maximum": 1 },
        "kind": { "enum": ["sampling", "parameter", "mixed"] },
        "points": {
          "type": "array",
          "minItems": 2,
          "items": {
            "type": "object",
            "required": ["loss", "pLower", "pUpper"],
            "properties": {
              "loss": { "type": "number", "minimum": 0 },
              "pLower": { "type": "number", "minimum": 0, "maximum": 1 },
              "pUpper": { "type": "number", "minimum": 0, "maximum": 1 }
            }
          }
        }
      }
    },
    "statistics": {
      "type": "object",
      "properties": {
        "mean": { "type": "number" },
        "median": { "type": "number" },
        "expectedAnnualLoss": { "type": "number" },
        "percentiles": {
          "type": "object",
          "patternProperties": {
            "^(0\\.(?!0+$)[0-9]+|[1-9][0-9]?(\\.[0-9]+)?)$": { "type": "number" }
          },
          "additionalProperties": false
        },
        "sampleCount": { "type": "integer", "minimum": 1 },
        "minLoss": { "type": "number" },
        "maxLoss": { "type": "number" }
      }
    },
    "referenceLine": {
      "type": "object",
      "required": ["axis", "value"],
      "properties": {
        "axis": { "enum": ["loss", "probability"] },
        "value": { "type": "number", "minimum": 0 },
        "label": { "type": "string" }
      }
    }
  }
}
