{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.getfounderhq.com/schemas/events-v2.json",
  "title": "FounderHQ Events v2 batch",
  "type": "object",
  "required": ["sent_at", "batch"],
  "additionalProperties": false,
  "properties": {
    "sent_at": { "type": "string", "format": "date-time" },
    "historical_migration": { "type": "boolean" },
    "batch": {
      "type": "array",
      "minItems": 1,
      "maxItems": 100,
      "items": { "$ref": "#/$defs/event" }
    }
  },
  "$defs": {
    "baseProperties": {
      "uuid": { "type": "string", "format": "uuid", "maxLength": 200 },
      "event": { "type": "string", "minLength": 1, "maxLength": 200 },
      "timestamp": { "type": "string", "format": "date-time" },
      "properties": { "type": "object" }
    },
    "event": {
      "type": "object",
      "required": ["uuid", "event", "distinct_id", "timestamp", "properties", "options"],
      "additionalProperties": false,
      "properties": {
        "uuid": { "$ref": "#/$defs/baseProperties/uuid" },
        "event": { "$ref": "#/$defs/baseProperties/event" },
        "distinct_id": { "type": "string", "minLength": 1, "maxLength": 400 },
        "timestamp": { "$ref": "#/$defs/baseProperties/timestamp" },
        "properties": { "$ref": "#/$defs/baseProperties/properties" },
        "session_id": { "type": "string", "maxLength": 200 },
        "window_id": { "type": "string", "maxLength": 200 },
        "options": {
          "type": "object",
          "required": ["process_person_profile"],
          "additionalProperties": false,
          "properties": {
            "process_person_profile": { "type": "boolean" },
            "cookieless_mode": { "type": "boolean" }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "options": {
                "properties": { "cookieless_mode": { "const": true } },
                "required": ["cookieless_mode"]
              }
            }
          },
          "then": {
            "properties": {
              "event": { "enum": ["$pageview", "$pageleave"] },
              "distinct_id": { "const": "$founderhq_cookieless" },
              "properties": { "$ref": "#/$defs/cookielessProperties" },
              "options": {
                "properties": {
                  "process_person_profile": { "const": false },
                  "cookieless_mode": { "const": true }
                }
              }
            },
            "not": {
              "anyOf": [
                { "required": ["session_id"] },
                { "required": ["window_id"] }
              ]
            }
          },
          "else": {
            "properties": {
              "distinct_id": {
                "not": { "const": "$founderhq_cookieless" }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "event": { "const": "$pageview" },
              "options": {
                "properties": { "cookieless_mode": { "const": true } },
                "required": ["cookieless_mode"]
              }
            }
          },
          "then": {
            "properties": {
              "properties": {
                "not": {
                  "anyOf": [
                    { "required": ["$page_duration_ms"] },
                    { "required": ["$max_scroll_percentage"] },
                    { "required": ["$leave_reason"] }
                  ]
                }
              }
            }
          }
        }
      ]
    },
    "cookielessProperties": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "$pathname": { "type": "string", "minLength": 1, "maxLength": 2000 },
        "$referring_domain": { "type": "string", "minLength": 1, "maxLength": 253 },
        "utm_source": { "type": "string", "maxLength": 200 },
        "utm_medium": { "type": "string", "maxLength": 200 },
        "utm_campaign": { "type": "string", "maxLength": 200 },
        "utm_term": { "type": "string", "maxLength": 200 },
        "utm_content": { "type": "string", "maxLength": 200 },
        "$platform": { "const": "web" },
        "$page_duration_ms": { "type": "integer", "minimum": 0, "maximum": 86400000 },
        "$max_scroll_percentage": { "type": "integer", "minimum": 0, "maximum": 100 },
        "$leave_reason": { "type": "string", "maxLength": 32 }
      }
    }
  }
}
