CREATE TABLE IF NOT EXISTS `la_ai_chat_session` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int unsigned NOT NULL DEFAULT 0 COMMENT '登录用户ID，匿名为0',
  `client_id` varchar(80) NOT NULL DEFAULT '' COMMENT '匿名客户端ID',
  `title` varchar(100) NOT NULL DEFAULT '' COMMENT '会话标题',
  `last_message` varchar(500) NOT NULL DEFAULT '' COMMENT '最后用户消息',
  `last_answer` varchar(500) NOT NULL DEFAULT '' COMMENT '最后助手回复',
  `message_count` int unsigned NOT NULL DEFAULT 0 COMMENT '消息数',
  `last_active_time` int unsigned NOT NULL DEFAULT 0 COMMENT '最后活跃时间',
  `create_time` int unsigned NOT NULL DEFAULT 0,
  `update_time` int unsigned NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_user_active` (`user_id`, `last_active_time`),
  KEY `idx_client_active` (`client_id`, `last_active_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI助手会话表';

CREATE TABLE IF NOT EXISTS `la_ai_chat_message` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `session_id` bigint unsigned NOT NULL DEFAULT 0 COMMENT '会话ID',
  `user_id` int unsigned NOT NULL DEFAULT 0 COMMENT '登录用户ID',
  `client_id` varchar(80) NOT NULL DEFAULT '' COMMENT '匿名客户端ID',
  `role` varchar(20) NOT NULL DEFAULT 'user' COMMENT 'user/assistant',
  `content` mediumtext NULL COMMENT '消息内容',
  `sources_json` json NULL COMMENT '引用来源',
  `model` varchar(100) NOT NULL DEFAULT '' COMMENT '模型',
  `tokens_used` int unsigned NOT NULL DEFAULT 0 COMMENT 'token消耗',
  `cost_ms` int unsigned NOT NULL DEFAULT 0 COMMENT '耗时毫秒',
  `status` tinyint unsigned NOT NULL DEFAULT 1 COMMENT '1成功2失败',
  `error_msg` varchar(500) NOT NULL DEFAULT '' COMMENT '错误信息',
  `create_time` int unsigned NOT NULL DEFAULT 0,
  `update_time` int unsigned NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_session_id` (`session_id`),
  KEY `idx_user_time` (`user_id`, `create_time`),
  KEY `idx_client_time` (`client_id`, `create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI助手消息表';

INSERT INTO `la_ai_config` (`name`, `value`, `remark`, `create_time`, `update_time`)
SELECT 'assistant_system_prompt', '你是世博头条 AI 助手，面向普通用户回答站内体育资讯、赛事、社区、彩票和加密行情相关问题。必须优先依据提供的站内资料和实时上下文回答；资料不足时要直接说明不足并建议用户换关键词。不要声称可以访问后台、源码、服务器或未提供的内部数据。回答使用中文，结构清晰，语气专业克制。涉及赛事预测、彩票或加密行情时，必须说明仅供参考，不构成投资或购彩建议。', '世博头条 AI 助手系统提示词', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (SELECT 1 FROM `la_ai_config` WHERE `name` = 'assistant_system_prompt');

INSERT INTO `la_ai_config` (`name`, `value`, `remark`, `create_time`, `update_time`)
SELECT 'assistant_kb_topk', '6', 'AI助手知识库召回数量', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (SELECT 1 FROM `la_ai_config` WHERE `name` = 'assistant_kb_topk');

INSERT INTO `la_ai_config` (`name`, `value`, `remark`, `create_time`, `update_time`)
SELECT 'assistant_history_limit', '8', 'AI助手发送给模型的历史消息数量', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (SELECT 1 FROM `la_ai_config` WHERE `name` = 'assistant_history_limit');

INSERT INTO `la_ai_config` (`name`, `value`, `remark`, `create_time`, `update_time`)
SELECT 'assistant_rate_limit_per_minute', '10', 'AI助手单用户/客户端每分钟请求上限', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (SELECT 1 FROM `la_ai_config` WHERE `name` = 'assistant_rate_limit_per_minute');

INSERT INTO `la_ai_config` (`name`, `value`, `remark`, `create_time`, `update_time`)
SELECT 'assistant_enable_crypto_realtime', '1', 'AI助手是否启用加密行情实时查询：1启用0关闭', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (SELECT 1 FROM `la_ai_config` WHERE `name` = 'assistant_enable_crypto_realtime');
