{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.funnelflux.pro/schemas/webhooks/webhook-outbound-payload-v1.schema.json",
  "title": "FunnelFlux Webhook Outbound Payload v1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schema_version", "event_type", "request_id", "timestamp"],
  "properties": {
    "schema_version": { "type": "string", "const": "1.0" },
    "event_type": {
      "type": "string",
      "enum": ["node_execution", "on_action", "conversion_triggered", "custom_event_triggered"]
    },
    "request_id": { "type": "string", "format": "uuid" },
    "timestamp": { "type": "integer", "minimum": 0 },
    "visitor": { "$ref": "#/$defs/visitor" },
    "funnel": { "$ref": "#/$defs/funnel" },
    "traffic_source": { "$ref": "#/$defs/traffic_source" },
    "hit": { "$ref": "#/$defs/hit" },
    "tracking_fields": { "$ref": "#/$defs/string_map" },
    "visitor_tags": {
      "type": "array",
      "items": { "type": "string" }
    },
    "session": { "$ref": "#/$defs/session" },
    "url_buffer": { "$ref": "#/$defs/string_map" },
    "is_action": { "type": "boolean" },
    "action_number": { "type": "integer", "minimum": 1 },
    "action_data": { "$ref": "#/$defs/action_data" },
    "conversion": { "$ref": "#/$defs/conversion" },
    "custom_event": { "$ref": "#/$defs/custom_event" }
  },
  "allOf": [
    {
      "if": { "properties": { "event_type": { "const": "conversion_triggered" } } },
      "then": {
        "required": ["conversion"],
        "not": {
          "anyOf": [
            { "required": ["session"] },
            { "required": ["url_buffer"] },
            { "required": ["is_action"] },
            { "required": ["action_number"] },
            { "required": ["action_data"] }
          ]
        }
      }
    },
    {
      "if": { "properties": { "event_type": { "const": "custom_event_triggered" } } },
      "then": {
        "required": ["custom_event"],
        "not": {
          "anyOf": [
            { "required": ["session"] },
            { "required": ["url_buffer"] },
            { "required": ["is_action"] },
            { "required": ["action_number"] },
            { "required": ["action_data"] }
          ]
        }
      }
    },
    {
      "if": { "properties": { "event_type": { "enum": ["node_execution", "on_action"] } } },
      "then": { "required": ["is_action", "action_data"] }
    }
  ],
  "$defs": {
    "string_map": {
      "type": "object",
      "additionalProperties": { "type": "string" }
    },
    "visitor": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "visitor_id": { "type": "string" },
        "ip": { "type": "string" },
        "user_agent": { "type": "string" },
        "country_code": { "type": "string" },
        "country_name": { "type": "string" },
        "region": { "type": "string" },
        "city": { "type": "string" },
        "timezone": { "type": "string" },
        "language": { "type": "string" },
        "device_type": { "type": "string" },
        "os": { "type": "string" },
        "browser": { "type": "string" },
        "connection_type": { "type": "string" }
      }
    },
    "funnel": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "funnel_id": { "type": "string" },
        "funnel_name": { "type": "string" },
        "node_id": { "type": "string" },
        "node_name": { "type": "string" }
      }
    },
    "traffic_source": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "traffic_source_id": { "type": "string" },
        "name": { "type": "string" }
      }
    },
    "hit": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "hit_id": { "type": "string" },
        "hit_timestamp": { "type": "integer", "minimum": 0 },
        "tracking_domain": { "type": "string" },
        "entrance_id": { "type": "string" },
        "entrance_timestamp": { "type": "integer", "minimum": 0 },
        "is_new_visitor": { "type": "boolean" },
        "is_entrance": { "type": "boolean" },
        "is_unique_node_view": { "type": "boolean" },
        "visitor_id": { "type": "string" },
        "traffic_source_id": { "type": "string" },
        "funnel_id": { "type": "string" },
        "node_id": { "type": "string" },
        "page_id": { "type": "string" },
        "page_group_id": { "type": "string" },
        "node_type": { "type": "string" },
        "nodes_path": {
          "type": "array",
          "items": { "type": "string" }
        },
        "time_on_page": { "type": "integer", "minimum": 0 },
        "referrer": { "type": "string" },
        "filtered_traffic": { "type": "boolean" },
        "tracking_fields": { "$ref": "#/$defs/string_map" }
      }
    },
    "session": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "visitor_id": { "type": "string" },
        "hit_id": { "type": "string" },
        "entrance_id": { "type": "string" },
        "entrance_timestamp": { "type": "integer", "minimum": 0 },
        "hit_timestamp": { "type": "integer", "minimum": 0 },
        "reference_hit_id": { "type": "string" },
        "most_recent_page_id": { "type": "string" },
        "referrer": { "type": "string" }
      }
    },
    "action_data": {
      "type": "object",
      "additionalProperties": {
        "type": ["string", "number", "boolean", "null"]
      }
    },
    "conversion": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "conversion_id": { "type": "string" },
        "hit_id": { "type": "string" },
        "transaction": { "type": "string" },
        "revenue": {},
        "conversion_timestamp": { "type": "integer", "minimum": 0 },
        "conversion_time": { "type": "integer", "minimum": 0 },
        "event_data1": { "type": "string" },
        "event_data2": { "type": "string" },
        "pii_email": { "type": "string" },
        "pii_phone": { "type": "string" },
        "passthrough_params": { "$ref": "#/$defs/string_map" }
      }
    },
    "custom_event": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "custom_event_id": { "type": "string" },
        "event_number": { "type": "integer", "minimum": 1 },
        "hit_id": { "type": "string" },
        "revenue": {},
        "timestamp": { "type": "integer", "minimum": 0 },
        "conversion_time": { "type": "integer", "minimum": 0 },
        "event_data1": { "type": "string" },
        "event_data2": { "type": "string" },
        "pii_email": { "type": "string" },
        "pii_phone": { "type": "string" },
        "passthrough_params": { "$ref": "#/$defs/string_map" }
      }
    }
  }
}
