{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.kiket.dev/workflow.json",
  "title": "Kiket Workflow Definition",
  "description": "Defines a workflow state machine with states, transitions, guards, and actions.",
  "type": "object",
  "required": [
    "model_version",
    "workflow",
    "states",
    "transitions"
  ],
  "properties": {
    "model_version": {
      "type": "string",
      "const": "1.0",
      "description": "Schema version, must be 1.0"
    },
    "workflow": {
      "type": "object",
      "required": [
        "name"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9_-]+$"
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "version": {
          "type": "string",
          "default": "1.0.0"
        },
        "description": {
          "type": "string"
        },
        "override": {
          "type": "boolean",
          "default": false
        },
        "settings": {
          "type": "object"
        }
      },
      "additionalProperties": false
    },
    "states": {
      "type": "object",
      "minProperties": 2,
      "description": "Must have at least one initial and one final state.",
      "additionalProperties": {
        "type": "object",
        "required": [
          "type"
        ],
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "initial",
              "active",
              "final",
              "approval",
              "gateway"
            ]
          },
          "category": {
            "type": "string",
            "enum": [
              "pending",
              "active",
              "review",
              "completed"
            ],
            "default": "active"
          },
          "metadata": {
            "type": "object",
            "properties": {
              "label": {
                "type": "string"
              },
              "color": {
                "type": "string"
              },
              "icon": {
                "type": "string"
              },
              "description": {
                "type": "string"
              }
            }
          },
          "wip_limit": {
            "type": "integer",
            "minimum": 0
          },
          "on_enter": {
            "$ref": "#/$defs/action_list"
          },
          "on_exit": {
            "$ref": "#/$defs/action_list"
          },
          "checklist": {
            "$ref": "#/$defs/checklist"
          },
          "sla": {
            "type": "object",
            "description": "SLA timer configuration for this state",
            "properties": {
              "warning": {
                "type": "string",
                "description": "Warning threshold duration (e.g. 4h, 24h, 72h)"
              },
              "breach": {
                "type": "string",
                "description": "Breach threshold duration (e.g. 8h, 48h, 168h)"
              },
              "business_hours": {
                "type": "boolean",
                "description": "Only count business hours toward SLA",
                "default": false
              },
              "on_warning": {
                "$ref": "#/$defs/action_list"
              },
              "on_breach": {
                "$ref": "#/$defs/action_list"
              }
            },
            "additionalProperties": false
          },
          "required_documents": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "key",
                "label"
              ],
              "properties": {
                "key": {
                  "type": "string"
                },
                "label": {
                  "type": "string"
                },
                "required": {
                  "type": "boolean",
                  "default": true
                },
                "accept": {
                  "type": "string"
                },
                "max_size": {
                  "type": "string"
                },
                "category": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    },
    "transitions": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [
          "from",
          "to"
        ],
        "properties": {
          "from": {
            "type": "string"
          },
          "to": {
            "oneOf": [
              {
                "type": "string"
              },
              {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            ]
          },
          "name": {
            "type": "string"
          },
          "auto": {
            "type": "boolean",
            "default": false
          },
          "trigger": {
            "type": "string"
          },
          "conditions": {},
          "guards": {
            "type": "array",
            "items": {
              "type": "object"
            }
          },
          "before": {
            "$ref": "#/$defs/action_list"
          },
          "after": {
            "$ref": "#/$defs/action_list"
          }
        }
      }
    },
    "variables": {
      "type": "object"
    },
    "integrations": {
      "type": "object"
    },
    "schedules": {
      "type": "object"
    }
  },
  "$defs": {
    "action_list": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string"
          },
          "config": {
            "type": "object"
          }
        }
      }
    },
    "checklist": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "title": {
          "type": "string"
        },
        "required": {
          "type": "string",
          "enum": [
            "all",
            "any",
            "threshold"
          ],
          "default": "all"
        },
        "threshold": {
          "type": "integer"
        },
        "items": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "label"
            ],
            "properties": {
              "id": {
                "type": "string"
              },
              "label": {
                "type": "string"
              },
              "check": {
                "type": "string",
                "enum": [
                  "manual",
                  "auto",
                  "event",
                  "webhook"
                ],
                "default": "manual"
              },
              "required": {
                "type": "boolean",
                "default": true
              }
            }
          }
        }
      }
    }
  },
  "additionalProperties": false
}
