-- 聊天消息日志表
-- 记录每一条发出和收到的消息，供 LLM 上下文拼接和调试

CREATE TABLE IF NOT EXISTS chat_messages (
  id          UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  chat_id     UUID NOT NULL REFERENCES customer_service_chats(id) ON DELETE CASCADE,
  round       INTEGER NOT NULL,
  direction   TEXT NOT NULL CHECK (direction IN ('SENT', 'RECEIVED')),
  content     TEXT NOT NULL DEFAULT '',
  message_hash TEXT,
  llm_intent  JSONB,
  created_at  TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_cm_chat_id
  ON chat_messages (chat_id, created_at);
