{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "bone-flow-working-definition.schema.json",
  "title": "Bone Flow Working/Draft Definition v1.0",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "state",
    "meta",
    "settings",
    "nodes",
    "joints",
    "sourceMap",
    "validation",
    "runtime"
  ],
  "properties": {
    "schemaVersion": {
      "const": "1.0"
    },
    "state": {
      "enum": [
        "draft",
        "validated",
        "pending"
      ]
    },
    "meta": {
      "type": "object"
    },
    "settings": {
      "type": "object"
    },
    "nodes": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/workingNode"
      }
    },
    "joints": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/workingJoint"
      }
    },
    "sourceMap": {
      "$ref": "#/$defs/sourceMap"
    },
    "validation": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/validationEntry"
      }
    },
    "runtime": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "$defs": {
    "sourceRef": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "line",
        "column",
        "startOffset",
        "endOffset",
        "raw"
      ],
      "properties": {
        "line": {
          "type": "integer",
          "minimum": 1
        },
        "column": {
          "type": "integer",
          "minimum": 1
        },
        "startOffset": {
          "type": "integer",
          "minimum": 0
        },
        "endOffset": {
          "type": "integer",
          "minimum": 0
        },
        "raw": {
          "type": "string"
        }
      }
    },
    "workingNode": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "id",
        "idOrigin",
        "type",
        "text",
        "properties",
        "position"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "idOrigin": {
          "enum": [
            "explicit",
            "auto"
          ]
        },
        "type": {
          "type": "string"
        },
        "text": {
          "type": "string"
        },
        "properties": {
          "type": "object"
        },
        "position": {
          "type": "object"
        }
      }
    },
    "workingSelector": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "kind",
        "raw",
        "role",
        "position",
        "fieldId"
      ],
      "properties": {
        "kind": {
          "enum": [
            "auto",
            "shape-role",
            "shape-position",
            "field",
            "invalid"
          ]
        },
        "raw": {
          "type": [
            "string",
            "null"
          ]
        },
        "role": {
          "type": [
            "string",
            "null"
          ]
        },
        "position": {
          "type": [
            "string",
            "null"
          ]
        },
        "fieldId": {
          "type": [
            "string",
            "null"
          ]
        }
      }
    },
    "workingEndpoint": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "nodeId",
        "selector"
      ],
      "properties": {
        "nodeId": {
          "type": "string"
        },
        "selector": {
          "$ref": "#/$defs/workingSelector"
        }
      }
    },
    "workingJoint": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "id",
        "from",
        "to",
        "label",
        "branchKey",
        "requested",
        "resolved"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "from": {
          "$ref": "#/$defs/workingEndpoint"
        },
        "to": {
          "$ref": "#/$defs/workingEndpoint"
        },
        "label": {
          "type": [
            "string",
            "null"
          ]
        },
        "branchKey": {
          "type": [
            "string",
            "null"
          ]
        },
        "requested": {
          "type": "object"
        },
        "resolved": {
          "type": [
            "object",
            "null"
          ]
        }
      }
    },
    "sourceMap": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "directives",
        "nodeDeclarations",
        "nodeReferences",
        "fields",
        "joints",
        "properties"
      ],
      "properties": {
        "directives": {
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/sourceRef"
            }
          }
        },
        "nodeDeclarations": {
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/sourceRef"
            }
          }
        },
        "nodeReferences": {
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/sourceRef"
            }
          }
        },
        "fields": {
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/sourceRef"
            }
          }
        },
        "joints": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/sourceRef"
          }
        },
        "properties": {
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/sourceRef"
            }
          }
        }
      }
    },
    "validationEntry": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "level",
        "code",
        "targetType",
        "targetId",
        "message",
        "provider",
        "source",
        "rootCause"
      ],
      "properties": {
        "level": {
          "enum": [
            "ERROR",
            "WARNING",
            "INFO"
          ]
        },
        "code": {
          "type": "string"
        },
        "targetType": {
          "type": [
            "string",
            "null"
          ]
        },
        "targetId": {
          "type": [
            "string",
            "null"
          ]
        },
        "message": {
          "type": "string"
        },
        "provider": {
          "enum": [
            "BoneFlowEditor",
            "BoneShape",
            "BoneJoint"
          ]
        },
        "source": {
          "oneOf": [
            {
              "$ref": "#/$defs/sourceRef"
            },
            {
              "type": "null"
            }
          ]
        },
        "rootCause": {
          "type": "boolean"
        }
      }
    }
  }
}
