{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://nahui.studio/schemas/connect/message.received.schema.json",
  "title": "Nahui Connect message.received",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "id",
    "type",
    "deliveryId",
    "projectId",
    "connectionId",
    "conversationId",
    "channel",
    "contact",
    "message",
    "receivedAt"
  ],
  "properties": {
    "id": { "type": "string", "pattern": "^nce_.+" },
    "type": { "const": "message.received" },
    "deliveryId": { "type": "string", "pattern": "^ncd_.+" },
    "projectId": { "type": "string", "pattern": "^ncp_.+" },
    "connectionId": { "type": "string", "pattern": "^ncc_.+" },
    "conversationId": { "type": "string", "pattern": "^ncv_.+", "maxLength": 160 },
    "channel": { "const": "whatsapp" },
    "contact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "displayName"],
      "properties": {
        "id": { "type": "string" },
        "displayName": { "type": "string", "maxLength": 160 }
      }
    },
    "message": {
      "oneOf": [
        { "$ref": "#/$defs/textMessage" },
        { "$ref": "#/$defs/buttonMessage" },
        { "$ref": "#/$defs/interactiveMessage" },
        { "$ref": "#/$defs/mediaMessage" },
        { "$ref": "#/$defs/unknownMessage" }
      ]
    },
    "receivedAt": { "type": "string", "format": "date-time" }
  },
  "$defs": {
    "baseMessageProperties": {
      "type": "object",
      "required": ["id", "type", "timestamp"],
      "properties": {
        "id": { "type": "string", "maxLength": 300 },
        "type": { "type": "string", "maxLength": 40 },
        "timestamp": { "type": "string", "format": "date-time" }
      }
    },
    "textMessage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "timestamp", "text"],
      "properties": {
        "id": { "type": "string", "maxLength": 300 },
        "type": { "const": "text" },
        "timestamp": { "type": "string", "format": "date-time" },
        "text": { "type": "string", "maxLength": 4000 }
      }
    },
    "buttonMessage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "timestamp", "button"],
      "properties": {
        "id": { "type": "string", "maxLength": 300 },
        "type": { "const": "button" },
        "timestamp": { "type": "string", "format": "date-time" },
        "button": { "type": "string", "maxLength": 1000 }
      }
    },
    "interactiveMessage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "timestamp", "interactive"],
      "properties": {
        "id": { "type": "string", "maxLength": 300 },
        "type": { "const": "interactive" },
        "timestamp": { "type": "string", "format": "date-time" },
        "interactive": {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "id", "title"],
          "properties": {
            "type": { "type": "string", "maxLength": 40 },
            "id": { "type": "string", "maxLength": 300 },
            "title": { "type": "string", "maxLength": 1000 }
          }
        }
      }
    },
    "mediaMessage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "timestamp", "media"],
      "properties": {
        "id": { "type": "string", "maxLength": 300 },
        "type": { "enum": ["image", "audio", "video", "document", "sticker"] },
        "timestamp": { "type": "string", "format": "date-time" },
        "media": {
          "type": "object",
          "additionalProperties": false,
          "required": ["id", "mimeType", "caption", "filename"],
          "properties": {
            "id": { "type": "string", "maxLength": 300 },
            "mimeType": { "type": "string", "maxLength": 160 },
            "caption": { "type": "string", "maxLength": 1024 },
            "filename": { "type": "string", "maxLength": 240 }
          }
        }
      }
    },
    "unknownMessage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "timestamp"],
      "properties": {
        "id": { "type": "string", "maxLength": 300 },
        "type": {
          "type": "string",
          "not": {
            "enum": ["text", "button", "interactive", "image", "audio", "video", "document", "sticker"]
          }
        },
        "timestamp": { "type": "string", "format": "date-time" }
      }
    }
  }
}
