{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://openworkplacehealth.org/schemas/v0.2/BenefitEntitlement.json",
  "title": "OWHS BenefitEntitlement v0.2",
  "description": "Declared statutory scheme or commercial workforce benefit. No eligibility, rate or legal entitlement is computed. Statutory descriptions are dated and sourced; omitted optional terms are not zero.",
  "type": "object",
  "properties": {
    "entitlementId": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$",
      "not": {
        "pattern": "\\s"
      }
    },
    "orgId": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$",
      "not": {
        "pattern": "\\s"
      }
    },
    "layer": {
      "type": "string",
      "enum": [
        "statutory",
        "commercial"
      ],
      "$comment": "codelist:benefit-layer@0.1.0"
    },
    "statutory": {
      "type": "object",
      "properties": {
        "scheme": {
          "type": "string",
          "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$",
          "not": {
            "pattern": "\\s"
          }
        },
        "eligibility": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2000
        },
        "waitingDays": {
          "type": "integer",
          "minimum": 0
        },
        "rate": {
          "type": "object",
          "properties": {
            "amount": {
              "type": "number",
              "minimum": 0
            },
            "currency": {
              "type": "string",
              "pattern": "^[A-Z]{3}$",
              "minLength": 3,
              "maxLength": 3,
              "$comment": "ISO 4217 code syntax only; membership is not resolved."
            },
            "basis": {
              "type": "string",
              "minLength": 1,
              "maxLength": 2000,
              "description": "Rate period or other calculation basis, for example per week. A variable statutory formula must be stated in description, not replaced with a fictitious fixed rate."
            },
            "description": {
              "type": "string",
              "minLength": 1,
              "maxLength": 2000
            }
          },
          "required": [
            "amount",
            "currency",
            "basis"
          ],
          "additionalProperties": false
        },
        "durationWeeks": {
          "type": "number",
          "minimum": 0
        },
        "sourceRef": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2000
        },
        "asOfDate": {
          "type": "string",
          "format": "date"
        }
      },
      "required": [
        "scheme",
        "eligibility",
        "sourceRef",
        "asOfDate"
      ],
      "additionalProperties": false
    },
    "productCategory": {
      "type": "string",
      "enum": [
        "PMI",
        "GIP",
        "GLA",
        "cash-plan",
        "EAP",
        "dental",
        "pension",
        "MSK-service",
        "wellbeing-intervention",
        "other"
      ],
      "$comment": "codelist:benefit-product@0.1.0"
    },
    "serviceName": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2000
    },
    "provider": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2000
    },
    "accessRoute": {
      "type": "string",
      "enum": [
        "self-referral",
        "manager",
        "gp",
        "oh",
        "hr"
      ],
      "$comment": "codelist:access-route@0.1.0"
    },
    "eligibilityScope": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2000
    },
    "healthDomainTags": {
      "type": "array",
      "items": {
        "type": "string",
        "enum": [
          "demands",
          "control",
          "support",
          "relationships",
          "role",
          "change",
          "mental-health",
          "physical-health",
          "musculoskeletal",
          "financial-wellbeing",
          "sleep-fatigue"
        ],
        "$comment": "codelist:construct-domain@0.1.0"
      },
      "minItems": 0,
      "uniqueItems": true
    },
    "ext": {
      "type": "object",
      "$comment": "Extension namespaces and values are structurally checked. The named identifier keys are prohibited recursively; OHEpisode also prohibits its named clinical-content keys recursively. This is not detection of identifiers or clinical meaning hidden in allowed keys or values. All extensions remain subject to P1-P5 and their applicable profile. A core-only pass does not establish profile or Level 3 conformance.",
      "propertyNames": {
        "pattern": "^owhs-[a-z][a-z0-9]*(?:-[a-z0-9]+)*$",
        "maxLength": 64
      },
      "additionalProperties": {
        "$ref": "#/$defs/owhsExtensionObject"
      }
    }
  },
  "required": [
    "entitlementId",
    "orgId",
    "layer"
  ],
  "additionalProperties": false,
  "$defs": {
    "owhsExtensionObject": {
      "type": "object",
      "propertyNames": {
        "not": {
          "enum": [
            "name",
            "nino",
            "email",
            "dateOfBirth",
            "address"
          ]
        }
      },
      "additionalProperties": {
        "$ref": "#/$defs/owhsExtensionValue"
      }
    },
    "owhsExtensionValue": {
      "anyOf": [
        {
          "type": [
            "string",
            "number",
            "boolean",
            "null"
          ]
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/owhsExtensionValue"
          }
        },
        {
          "$ref": "#/$defs/owhsExtensionObject"
        }
      ]
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "layer": {
            "const": "statutory"
          }
        },
        "required": [
          "layer"
        ]
      },
      "then": {
        "required": [
          "statutory"
        ],
        "not": {
          "anyOf": [
            {
              "required": [
                "productCategory"
              ]
            }
          ]
        }
      },
      "else": {
        "required": [
          "productCategory"
        ],
        "not": {
          "anyOf": [
            {
              "required": [
                "statutory"
              ]
            }
          ]
        }
      }
    }
  ]
}
