{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/samva-dev/json-schema/samva-dev-message-schema.json",
  "title": "Message",
  "x-generated": "2026-10-31",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/samva-dev-openapi.json#/components/schemas/Message",
  "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"
        }
      ]
    },
    "channel": {
      "type": "string",
      "enum": [
        "email"
      ]
    },
    "direction": {
      "type": "string",
      "enum": [
        "outbound",
        "inbound"
      ]
    },
    "status": {
      "type": "string",
      "enum": [
        "pending",
        "scheduled",
        "queued",
        "processing",
        "sent",
        "delivered",
        "read",
        "failed",
        "bounced",
        "complained"
      ]
    },
    "from": {
      "anyOf": [
        {
          "$ref": "#/$defs/MessageSender"
        },
        {
          "type": "null"
        }
      ]
    },
    "to": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/MessageAddress"
      }
    },
    "cc": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/MessageAddress"
      }
    },
    "bcc": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/MessageAddress"
      }
    },
    "replyTo": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "subject": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "conversationId": {
      "anyOf": [
        {
          "$ref": "#/$defs/ConversationId"
        },
        {
          "type": "null"
        }
      ]
    },
    "inReplyToMessageId": {
      "anyOf": [
        {
          "$ref": "#/$defs/MessageId"
        },
        {
          "type": "null"
        }
      ]
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "metadata": {
      "anyOf": [
        {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/JsonValue"
          }
        },
        {
          "type": "null"
        }
      ]
    },
    "isTest": {
      "type": "boolean"
    },
    "scheduledAt": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "submission": {
      "anyOf": [
        {
          "$ref": "#/$defs/MessageSubmission"
        },
        {
          "type": "null"
        }
      ]
    },
    "waitReason": {
      "anyOf": [
        {
          "type": "string",
          "enum": [
            "rate",
            "daily-quota",
            "domain-health",
            "sending-paused",
            "upstream-unavailable",
            "retry",
            "campaign-paused",
            "domain-warming",
            "onboarding-review",
            "content-review",
            "screening-unavailable"
          ]
        },
        {
          "type": "null"
        }
      ]
    },
    "reviewExpectedAt": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "skippedRecipients": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/SkippedRecipient"
      }
    },
    "createdAt": {
      "type": "string"
    },
    "updatedAt": {
      "type": "string"
    }
  },
  "required": [
    "id",
    "channel",
    "direction",
    "status",
    "from",
    "to",
    "cc",
    "bcc",
    "replyTo",
    "subject",
    "conversationId",
    "inReplyToMessageId",
    "tags",
    "metadata",
    "isTest",
    "scheduledAt",
    "submission",
    "waitReason",
    "reviewExpectedAt",
    "skippedRecipients",
    "createdAt",
    "updatedAt"
  ],
  "additionalProperties": false,
  "$defs": {
    "ApiKeyId": {
      "type": "string",
      "pattern": "^key_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "ApiKeyId"
    },
    "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"
    },
    "ContactId": {
      "type": "string",
      "pattern": "^contact_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "ContactId"
    },
    "ConversationId": {
      "type": "string",
      "pattern": "^conv_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "ConversationId"
    },
    "JsonValue": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "number"
        },
        {
          "type": "boolean"
        },
        {
          "type": "null"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/JsonValue"
          }
        },
        {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/JsonValue"
          }
        }
      ]
    },
    "MessageAddress": {
      "type": "object",
      "properties": {
        "address": {
          "type": "string"
        },
        "contactId": {
          "anyOf": [
            {
              "$ref": "#/$defs/ContactId"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "address",
        "contactId"
      ],
      "additionalProperties": false
    },
    "MessageId": {
      "type": "string",
      "pattern": "^msg_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "MessageId"
    },
    "MessageSender": {
      "type": "object",
      "properties": {
        "address": {
          "type": "string"
        },
        "name": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "address",
        "name"
      ],
      "additionalProperties": false
    },
    "MessageSubmission": {
      "type": "object",
      "properties": {
        "origin": {
          "type": "string",
          "enum": [
            "direct",
            "campaign",
            "scheduled",
            "mailbox",
            "forwarding",
            "journey",
            "businessApp"
          ]
        },
        "transport": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "http"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "credential": {
          "anyOf": [
            {
              "$ref": "#/$defs/SubmissionCredential"
            },
            {
              "type": "null"
            }
          ]
        },
        "userId": {
          "anyOf": [
            {
              "$ref": "#/$defs/UserId"
            },
            {
              "type": "null"
            }
          ]
        },
        "client": {
          "anyOf": [
            {
              "$ref": "#/$defs/SubmissionClient"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "origin",
        "transport",
        "credential",
        "userId",
        "client"
      ],
      "additionalProperties": false
    },
    "SkippedRecipient": {
      "type": "object",
      "properties": {
        "address": {
          "type": "string"
        },
        "reason": {
          "type": "string",
          "enum": [
            "bounce",
            "complaint",
            "unsubscribe",
            "manual",
            "topic-unsubscribe"
          ]
        },
        "topicId": {
          "anyOf": [
            {
              "$ref": "#/$defs/TopicId"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "address",
        "reason"
      ],
      "additionalProperties": false
    },
    "SubmissionClient": {
      "type": "object",
      "properties": {
        "surface": {
          "type": "string",
          "enum": [
            "typescript-promise",
            "typescript-effect",
            "cli",
            "mcp"
          ]
        },
        "version": {
          "type": "string"
        }
      },
      "required": [
        "surface",
        "version"
      ],
      "additionalProperties": false
    },
    "SubmissionCredential": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "enum": [
                "api_key"
              ]
            },
            "apiKeyId": {
              "anyOf": [
                {
                  "$ref": "#/$defs/ApiKeyId"
                },
                {
                  "type": "null"
                }
              ]
            }
          },
          "required": [
            "kind",
            "apiKeyId"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "enum": [
                "oauth"
              ]
            },
            "clientId": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "null"
                }
              ]
            }
          },
          "required": [
            "kind",
            "clientId"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "enum": [
                "session"
              ]
            }
          },
          "required": [
            "kind"
          ],
          "additionalProperties": false
        }
      ]
    },
    "TopicId": {
      "type": "string",
      "pattern": "^unsub_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "TopicId"
    },
    "UserId": {
      "type": "string",
      "pattern": "^user_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "UserId"
    }
  }
}