-- SentClaw 数据库初始化脚本
-- 创建时间: 2026-04-07

-- 设置字符集
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- 用户表
CREATE TABLE IF NOT EXISTS `users` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
  `username` varchar(50) NOT NULL COMMENT '用户名',
  `password` varchar(255) NOT NULL COMMENT '密码（加密）',
  `email` varchar(100) DEFAULT NULL COMMENT '邮箱',
  `avatar` varchar(255) DEFAULT NULL COMMENT '头像URL',
  `is_active` tinyint(1) DEFAULT 1 COMMENT '是否激活',
  `is_admin` tinyint(1) DEFAULT 0 COMMENT '是否管理员',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_username` (`username`),
  UNIQUE KEY `uk_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

-- 工作空间表
CREATE TABLE IF NOT EXISTS `workspaces` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '工作空间ID',
  `user_id` bigint(20) NOT NULL COMMENT '用户ID',
  `name` varchar(100) NOT NULL COMMENT '工作空间名称',
  `description` text COMMENT '描述',
  `is_default` tinyint(1) DEFAULT 0 COMMENT '是否默认',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  CONSTRAINT `fk_workspaces_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='工作空间表';

-- Agent 表
CREATE TABLE IF NOT EXISTS `agents` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Agent ID',
  `workspace_id` bigint(20) NOT NULL COMMENT '工作空间ID',
  `name` varchar(100) NOT NULL COMMENT 'Agent 名称',
  `description` text COMMENT '描述',
  `system_prompt` text COMMENT '系统提示词',
  `model_id` varchar(50) DEFAULT NULL COMMENT '模型ID',
  `temperature` decimal(3,2) DEFAULT 0.70 COMMENT '温度参数',
  `max_tokens` int(11) DEFAULT 2000 COMMENT '最大Token数',
  `is_active` tinyint(1) DEFAULT 1 COMMENT '是否激活',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_workspace_id` (`workspace_id`),
  CONSTRAINT `fk_agents_workspace` FOREIGN KEY (`workspace_id`) REFERENCES `workspaces` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Agent 表';

-- 会话表
CREATE TABLE IF NOT EXISTS `conversations` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '会话ID',
  `workspace_id` bigint(20) NOT NULL COMMENT '工作空间ID',
  `agent_id` bigint(20) NOT NULL COMMENT 'Agent ID',
  `title` varchar(200) DEFAULT NULL COMMENT '会话标题',
  `channel` varchar(50) DEFAULT 'web' COMMENT '渠道（web, dingtalk, feishu, telegram, discord）',
  `channel_user_id` varchar(100) DEFAULT NULL COMMENT '渠道用户ID',
  `status` varchar(20) DEFAULT 'active' COMMENT '状态（active, archived, deleted）',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_workspace_id` (`workspace_id`),
  KEY `idx_agent_id` (`agent_id`),
  KEY `idx_channel` (`channel`),
  CONSTRAINT `fk_conversations_workspace` FOREIGN KEY (`workspace_id`) REFERENCES `workspaces` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_conversations_agent` FOREIGN KEY (`agent_id`) REFERENCES `agents` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会话表';

-- 消息表
CREATE TABLE IF NOT EXISTS `messages` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '消息ID',
  `conversation_id` bigint(20) NOT NULL COMMENT '会话ID',
  `role` varchar(20) NOT NULL COMMENT '角色（user, assistant, system）',
  `content` text NOT NULL COMMENT '消息内容',
  `tokens` int(11) DEFAULT NULL COMMENT 'Token数',
  `model` varchar(50) DEFAULT NULL COMMENT '使用的模型',
  `tool_calls` json DEFAULT NULL COMMENT '工具调用记录',
  `metadata` json DEFAULT NULL COMMENT '元数据',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_conversation_id` (`conversation_id`),
  KEY `idx_created_at` (`created_at`),
  CONSTRAINT `fk_messages_conversation` FOREIGN KEY (`conversation_id`) REFERENCES `conversations` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='消息表';

-- 工具表
CREATE TABLE IF NOT EXISTS `tools` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '工具ID',
  `name` varchar(100) NOT NULL COMMENT '工具名称',
  `type` varchar(50) NOT NULL COMMENT '类型（builtin, mcp, custom）',
  `description` text COMMENT '描述',
  `config` json DEFAULT NULL COMMENT '配置（JSON）',
  `is_active` tinyint(1) DEFAULT 1 COMMENT '是否激活',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='工具表';

-- Agent 工具关联表
CREATE TABLE IF NOT EXISTS `agent_tools` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `agent_id` bigint(20) NOT NULL COMMENT 'Agent ID',
  `tool_id` bigint(20) NOT NULL COMMENT '工具ID',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_agent_tool` (`agent_id`, `tool_id`),
  CONSTRAINT `fk_agent_tools_agent` FOREIGN KEY (`agent_id`) REFERENCES `agents` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_agent_tools_tool` FOREIGN KEY (`tool_id`) REFERENCES `tools` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Agent 工具关联表';

-- 技能表
CREATE TABLE IF NOT EXISTS `skills` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '技能ID',
  `name` varchar(100) NOT NULL COMMENT '技能名称',
  `version` varchar(20) DEFAULT '1.0.0' COMMENT '版本',
  `description` text COMMENT '描述',
  `author` varchar(100) DEFAULT NULL COMMENT '作者',
  `repository` varchar(255) DEFAULT NULL COMMENT '仓库地址',
  `config` json DEFAULT NULL COMMENT '配置（JSON）',
  `is_installed` tinyint(1) DEFAULT 0 COMMENT '是否已安装',
  `is_active` tinyint(1) DEFAULT 1 COMMENT '是否激活',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_name_version` (`name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='技能表';

-- 记忆表
CREATE TABLE IF NOT EXISTS `memories` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '记忆ID',
  `workspace_id` bigint(20) NOT NULL COMMENT '工作空间ID',
  `agent_id` bigint(20) DEFAULT NULL COMMENT 'Agent ID',
  `type` varchar(50) NOT NULL COMMENT '类型（profile, memory, note）',
  `content` text NOT NULL COMMENT '内容',
  `tags` json DEFAULT NULL COMMENT '标签',
  `importance` int(11) DEFAULT 5 COMMENT '重要性（1-10）',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_workspace_id` (`workspace_id`),
  KEY `idx_agent_id` (`agent_id`),
  KEY `idx_type` (`type`),
  CONSTRAINT `fk_memories_workspace` FOREIGN KEY (`workspace_id`) REFERENCES `workspaces` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_memories_agent` FOREIGN KEY (`agent_id`) REFERENCES `agents` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='记忆表';

-- 模型配置表
CREATE TABLE IF NOT EXISTS `models` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '模型ID',
  `provider` varchar(50) NOT NULL COMMENT '提供商（dashscope, openai, anthropic, etc）',
  `name` varchar(100) NOT NULL COMMENT '模型名称',
  `model_id` varchar(100) NOT NULL COMMENT '模型ID（如 qwen-plus）',
  `api_key` varchar(255) DEFAULT NULL COMMENT 'API Key',
  `base_url` varchar(255) DEFAULT NULL COMMENT 'API Base URL',
  `is_default` tinyint(1) DEFAULT 0 COMMENT '是否默认',
  `is_active` tinyint(1) DEFAULT 1 COMMENT '是否激活',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_provider_model` (`provider`, `model_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='模型配置表';

-- 定时任务表
CREATE TABLE IF NOT EXISTS `cron_jobs` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '任务ID',
  `agent_id` bigint(20) NOT NULL COMMENT 'Agent ID',
  `name` varchar(100) NOT NULL COMMENT '任务名称',
  `cron_expression` varchar(50) NOT NULL COMMENT 'Cron 表达式',
  `prompt` text NOT NULL COMMENT '执行提示词',
  `is_active` tinyint(1) DEFAULT 1 COMMENT '是否激活',
  `last_run_at` timestamp NULL DEFAULT NULL COMMENT '上次运行时间',
  `next_run_at` timestamp NULL DEFAULT NULL COMMENT '下次运行时间',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_agent_id` (`agent_id`),
  CONSTRAINT `fk_cron_jobs_agent` FOREIGN KEY (`agent_id`) REFERENCES `agents` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='定时任务表';

-- 渠道配置表
CREATE TABLE IF NOT EXISTS `channels` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '渠道ID',
  `type` varchar(50) NOT NULL COMMENT '类型（web, dingtalk, feishu, telegram, discord, qq）',
  `name` varchar(100) NOT NULL COMMENT '渠道名称',
  `config` json NOT NULL COMMENT '配置（JSON）',
  `is_active` tinyint(1) DEFAULT 1 COMMENT '是否激活',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='渠道配置表';

-- 插入默认管理员用户（密码: admin123，需要加密）
INSERT INTO `users` (`username`, `password`, `email`, `is_admin`) VALUES
('admin', '$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5NU7qP.9YwM1K', 'admin@sentclaw.com', 1);

-- 插入默认模型配置
INSERT INTO `models` (`provider`, `name`, `model_id`, `is_default`, `is_active`) VALUES
('dashscope', 'Qwen Plus', 'qwen-plus', 1, 1),
('dashscope', 'Qwen Turbo', 'qwen-turbo', 0, 1),
('openai', 'GPT-4', 'gpt-4', 0, 1),
('openai', 'GPT-3.5 Turbo', 'gpt-3.5-turbo', 0, 1);

SET FOREIGN_KEY_CHECKS = 1;
