-- 消息表
CREATE TABLE IF NOT EXISTS messages (
    id          BIGSERIAL PRIMARY KEY,
    session_id  UUID NOT NULL,
    role        VARCHAR(16) NOT NULL,  -- "user" | "assistant" | "system"
    content     TEXT NOT NULL,
    tokens_used INTEGER NOT NULL DEFAULT 0,
    created_at  TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 按会话查询消息（分页核心索引）
CREATE INDEX IF NOT EXISTS idx_messages_session_id_created_at
    ON messages(session_id, created_at);

-- 按会话查询最后一条消息
CREATE INDEX IF NOT EXISTS idx_messages_session_id_id_desc
    ON messages(session_id, id DESC);
