CREATE TABLE IF NOT EXISTS webchat_messages (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  message_id TEXT NOT NULL UNIQUE,
  session_id TEXT NOT NULL,
  text TEXT NOT NULL,
  visitor_name TEXT NULL,
  customer_external_id TEXT NULL,
  interaction_id TEXT NULL,
  queue_id TEXT NULL,
  priority INTEGER NOT NULL DEFAULT 3,
  payload_json TEXT NOT NULL DEFAULT '{}',
  created_at TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS ix_webchat_messages_message_id ON webchat_messages (message_id);
CREATE INDEX IF NOT EXISTS ix_webchat_messages_session_id ON webchat_messages (session_id);
CREATE INDEX IF NOT EXISTS ix_webchat_messages_customer_external_id ON webchat_messages (customer_external_id);
CREATE INDEX IF NOT EXISTS ix_webchat_messages_interaction_id ON webchat_messages (interaction_id);
CREATE INDEX IF NOT EXISTS ix_webchat_messages_queue_id ON webchat_messages (queue_id);
CREATE INDEX IF NOT EXISTS ix_webchat_messages_priority ON webchat_messages (priority);
CREATE INDEX IF NOT EXISTS ix_webchat_messages_created_at ON webchat_messages (created_at);
