{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/samva-dev/json-schema/samva-dev-mailbox-message-schema.json",
  "title": "MailboxMessage",
  "x-generated": "2026-10-31",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/samva-dev-openapi.json#/components/schemas/MailboxMessage",
  "type": "object",
  "properties": {
    "id": {
      "$ref": "#/$defs/MessageId"
    },
    "campaign": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "campaignId": {
              "$ref": "#/$defs/CampaignId"
            },
            "runId": {
              "$ref": "#/$defs/CampaignRunId"
            }
          },
          "required": [
            "campaignId",
            "runId"
          ],
          "additionalProperties": false
        },
        {
          "type": "null"
        }
      ]
    },
    "threadId": {
      "$ref": "#/$defs/MailboxThreadId"
    },
    "conversationId": {
      "$ref": "#/$defs/ConversationId"
    },
    "direction": {
      "type": "string",
      "enum": [
        "inbound",
        "outbound"
      ]
    },
    "from": {
      "$ref": "#/$defs/MailboxMessageSender"
    },
    "sentBy": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "enum": [
                "user",
                "apiKey",
                "extension",
                "externalAgent",
                "samvaAgent",
                "system"
              ]
            },
            "id": {
              "type": "string"
            },
            "displayName": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "null"
                }
              ]
            }
          },
          "required": [
            "kind",
            "id"
          ],
          "additionalProperties": false
        },
        {
          "type": "null"
        }
      ]
    },
    "occurredAt": {
      "type": "string"
    },
    "channel": {
      "type": "string",
      "enum": [
        "email"
      ]
    },
    "recipients": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/MailboxMessageRecipient"
      }
    },
    "replyTo": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "subject": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "text": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "html": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "replyText": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "headers": {
      "type": "object",
      "properties": {
        "messageId": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ]
        },
        "inReplyTo": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ]
        },
        "references": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": [
        "messageId",
        "inReplyTo",
        "references"
      ],
      "additionalProperties": false
    },
    "authentication": {
      "anyOf": [
        {
          "$ref": "#/$defs/MailboxMessageAuthentication"
        },
        {
          "type": "null"
        }
      ]
    },
    "attachments": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "$ref": "#/$defs/AttachmentId"
          },
          "filename": {
            "type": "string"
          },
          "contentType": {
            "type": "string"
          },
          "size": {
            "type": "integer",
            "minimum": 0,
            "maximum": 9007199254740991
          },
          "isInline": {
            "type": "boolean"
          }
        },
        "required": [
          "id",
          "filename",
          "contentType",
          "size",
          "isInline"
        ],
        "additionalProperties": false
      }
    }
  },
  "required": [
    "id",
    "threadId",
    "conversationId",
    "direction",
    "from",
    "occurredAt",
    "channel",
    "recipients",
    "replyTo",
    "headers",
    "authentication",
    "attachments"
  ],
  "additionalProperties": false,
  "$defs": {
    "AttachmentId": {
      "type": "string",
      "pattern": "^att_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "AttachmentId"
    },
    "CampaignId": {
      "type": "string",
      "pattern": "^camp_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "CampaignId"
    },
    "CampaignRunId": {
      "type": "string",
      "pattern": "^crun_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "CampaignRunId"
    },
    "ConversationId": {
      "type": "string",
      "pattern": "^conv_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "ConversationId"
    },
    "MailboxMessageAuthentication": {
      "type": "object",
      "properties": {
        "spf": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ]
        },
        "dkim": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ]
        },
        "dmarc": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "spf",
        "dkim",
        "dmarc"
      ],
      "additionalProperties": false
    },
    "MailboxMessageRecipient": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "enum": [
            "to",
            "cc",
            "bcc"
          ]
        },
        "actor": {
          "anyOf": [
            {
              "type": "object",
              "properties": {
                "kind": {
                  "type": "string",
                  "enum": [
                    "user",
                    "apiKey",
                    "extension",
                    "externalAgent",
                    "samvaAgent",
                    "system"
                  ]
                },
                "id": {
                  "type": "string"
                },
                "displayName": {
                  "anyOf": [
                    {
                      "type": "string"
                    },
                    {
                      "type": "null"
                    }
                  ]
                }
              },
              "required": [
                "kind",
                "id"
              ],
              "additionalProperties": false
            },
            {
              "type": "null"
            }
          ]
        },
        "displayName": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ]
        },
        "address": {
          "type": "string"
        }
      },
      "required": [
        "kind",
        "address"
      ],
      "additionalProperties": false
    },
    "MailboxMessageSender": {
      "type": "object",
      "properties": {
        "actor": {
          "anyOf": [
            {
              "type": "object",
              "properties": {
                "kind": {
                  "type": "string",
                  "enum": [
                    "user",
                    "apiKey",
                    "extension",
                    "externalAgent",
                    "samvaAgent",
                    "system"
                  ]
                },
                "id": {
                  "type": "string"
                },
                "displayName": {
                  "anyOf": [
                    {
                      "type": "string"
                    },
                    {
                      "type": "null"
                    }
                  ]
                }
              },
              "required": [
                "kind",
                "id"
              ],
              "additionalProperties": false
            },
            {
              "type": "null"
            }
          ]
        },
        "displayName": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ]
        },
        "address": {
          "type": "string"
        }
      },
      "required": [
        "address"
      ],
      "additionalProperties": false
    },
    "MailboxThreadId": {
      "type": "string",
      "pattern": "^thread_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "MailboxThreadId"
    },
    "MessageId": {
      "type": "string",
      "pattern": "^msg_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "MessageId"
    }
  }
}