{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kbx.local/contracts/problems/kbx.problem.schema.json",
  "title": "KBX Problem",
  "oneOf": [
    { "$ref": "#/$defs/validation" },
    { "$ref": "#/$defs/businessRule" },
    { "$ref": "#/$defs/conflict" },
    { "$ref": "#/$defs/permission" },
    { "$ref": "#/$defs/notFound" },
    { "$ref": "#/$defs/integration" },
    { "$ref": "#/$defs/system" }
  ],
  "$defs": {
    "base": {
      "type": "object",
      "required": ["type", "title"],
      "properties": {
        "type": { "type": "string" },
        "title": { "type": "string", "minLength": 1 },
        "detail": { "type": ["string", "null"] },
        "correlationId": { "type": ["string", "null"] }
      }
    },
    "action": {
      "type": "object",
      "required": ["id", "label"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "label": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "validationError": {
      "type": "object",
      "required": ["code", "message"],
      "properties": {
        "field": { "type": ["string", "null"] },
        "rowKey": { "type": ["string", "null"] },
        "code": { "type": "string", "minLength": 1 },
        "message": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "validation": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "type": "object",
          "required": ["type", "errors"],
          "properties": {
            "type": { "const": "validation" },
            "errors": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/validationError" } }
          }
        }
      ]
    },
    "businessRule": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "type": "object",
          "required": ["type", "code"],
          "properties": {
            "type": { "const": "business-rule" },
            "code": { "type": "string", "minLength": 1 },
            "actions": { "type": "array", "items": { "$ref": "#/$defs/action" } }
          }
        }
      ]
    },
    "conflict": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "type": "object",
          "required": ["type", "code"],
          "properties": {
            "type": { "const": "conflict" },
            "code": { "type": "string", "minLength": 1 },
            "currentVersion": { "type": ["integer", "null"], "minimum": 1 }
          }
        }
      ]
    },
    "permission": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "type": "object",
          "required": ["type", "code"],
          "properties": {
            "type": { "const": "permission" },
            "code": { "type": "string", "minLength": 1 }
          }
        }
      ]
    },
    "notFound": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "type": "object",
          "required": ["type", "code"],
          "properties": {
            "type": { "const": "not-found" },
            "code": { "type": "string", "minLength": 1 }
          }
        }
      ]
    },
    "integration": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "type": "object",
          "required": ["type", "code", "retryable"],
          "properties": {
            "type": { "const": "integration" },
            "code": { "type": "string", "minLength": 1 },
            "retryable": { "type": "boolean" }
          }
        }
      ]
    },
    "system": {
      "allOf": [
        { "$ref": "#/$defs/base" },
        {
          "type": "object",
          "required": ["type", "code", "correlationId"],
          "properties": {
            "type": { "const": "system" },
            "code": { "type": "string", "minLength": 1 },
            "correlationId": { "type": "string", "minLength": 1 },
            "retryable": { "type": "boolean" }
          }
        }
      ]
    }
  }
}
