-- 添加通用对话表
-- 执行日期: 2025-10-11
-- 用途: 支持无项目/无智能体的纯AI对话功能

-- 1. 创建通用对话表
CREATE TABLE IF NOT EXISTS general_conversations (
  id VARCHAR(36) PRIMARY KEY,
  user_id VARCHAR(36) NOT NULL,
  title VARCHAR(255) NOT NULL,
  model_name VARCHAR(50),
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  deleted_at TIMESTAMP,
  
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- 2. 创建通用消息表
CREATE TABLE IF NOT EXISTS general_messages (
  id VARCHAR(36) PRIMARY KEY,
  conversation_id VARCHAR(36) NOT NULL,
  role VARCHAR(20) NOT NULL,
  content TEXT NOT NULL,
  model VARCHAR(50),
  metadata JSONB,
  tokens INTEGER,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  
  FOREIGN KEY (conversation_id) REFERENCES general_conversations(id) ON DELETE CASCADE
);

-- 3. 创建索引
CREATE INDEX IF NOT EXISTS idx_general_conversations_user_id ON general_conversations(user_id);
CREATE INDEX IF NOT EXISTS idx_general_conversations_created_at ON general_conversations(created_at);
CREATE INDEX IF NOT EXISTS idx_general_conversations_updated_at ON general_conversations(updated_at);

CREATE INDEX IF NOT EXISTS idx_general_messages_conversation_id ON general_messages(conversation_id);
CREATE INDEX IF NOT EXISTS idx_general_messages_created_at ON general_messages(created_at);

-- 4. 验证表已创建
SELECT 'general_conversations表已创建' AS status 
WHERE EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'general_conversations');

SELECT 'general_messages表已创建' AS status 
WHERE EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'general_messages');


