{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://sendoracloud.com/schemas/workflow-bundle.v1.json",
  "title": "Sendora Workflow Bundle",
  "description": "Versioned, IaC-friendly bundle of one or more Sendora automation workflows. Output of GET /orgs/:orgId/automation/workflows/export — input to POST /orgs/:orgId/automation/workflows/import. Strips org/project IDs + per-instance metadata so bundles are portable across orgs and projects.",
  "type": "object",
  "required": ["version", "exportedAt", "workflows"],
  "additionalProperties": false,
  "properties": {
    "version": {
      "const": 1,
      "description": "Bundle format version. Importer rejects unknown versions."
    },
    "exportedAt": {
      "type": "string",
      "format": "date-time",
      "description": "ISO 8601 timestamp of export — informational, not validated on import."
    },
    "workflows": {
      "type": "array",
      "items": { "$ref": "#/$defs/workflow" }
    }
  },
  "$defs": {
    "workflow": {
      "type": "object",
      "required": ["name", "description", "trigger", "steps", "isActive"],
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200,
          "description": "Unique within the target project. Importer matches by name — same name = update in place; new name = create."
        },
        "description": {
          "type": ["string", "null"],
          "maxLength": 2000
        },
        "trigger": { "$ref": "#/$defs/trigger" },
        "steps": {
          "type": "array",
          "items": { "$ref": "#/$defs/step" }
        },
        "isActive": {
          "type": "boolean",
          "description": "Inactive workflows are ignored by the evaluator. Useful for shipping a workflow disabled then enabling via dashboard once verified."
        }
      }
    },
    "trigger": {
      "type": "object",
      "required": ["eventType"],
      "additionalProperties": false,
      "properties": {
        "eventType": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128,
          "description": "Literal event name to match (e.g. 'order.shipped'). Wildcards not supported — use a workflow per event."
        },
        "filters": {
          "type": "object",
          "description": "Map of event-property path → required string value. Top-level shallow only. AND-combined with eventType.",
          "additionalProperties": { "type": "string" }
        },
        "audienceId": {
          "type": "string",
          "format": "uuid",
          "description": "Optional — restrict trigger to users in this audience at event time."
        }
      }
    },
    "step": {
      "type": "object",
      "required": ["type", "config"],
      "additionalProperties": false,
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "send_email",
            "send_push",
            "send_sms",
            "update_profile",
            "webhook",
            "branch",
            "wait",
            "ai_action"
          ]
        },
        "config": {
          "type": "object",
          "description": "Step-specific config. Shape depends on type — see https://sendoracloud.com/docs/api#workflows for per-type schemas."
        },
        "delayMinutes": {
          "type": "integer",
          "minimum": 0,
          "maximum": 525600,
          "description": "Optional. Minutes to wait before executing this step. Implemented via workflow_runs.next_step_at."
        }
      }
    }
  }
}
