-- =============================================================================
--  ai_message
--
--  One row per turn in an ai_conversation. Role is one of: system, user,
--  assistant, tool. token_count is best-effort (filled in by the plugin
--  when the model returns usage info; 0 otherwise).
-- =============================================================================

CREATE TABLE IF NOT EXISTS ai_message (
    ai_message_id              INT(11)     NOT NULL AUTO_INCREMENT,
    ai_message_conversation_id INT(11)     NOT NULL,
    ai_message_role            ENUM('system','user','assistant','tool') NOT NULL,
    ai_message_content         MEDIUMTEXT  NOT NULL,
    ai_message_tool_name       VARCHAR(64) NULL,
    ai_message_token_count     INT(11)     NOT NULL DEFAULT 0,
    ai_message_created_at      TIMESTAMP   NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (ai_message_id),
    KEY ai_message_conversation_idx (ai_message_conversation_id),
    CONSTRAINT ai_message_conversation_fk
        FOREIGN KEY (ai_message_conversation_id)
        REFERENCES ai_conversation (ai_conversation_id)
        ON DELETE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
