-- 云屋平台 - 数据库模式
-- 版本: 3.0
-- 描述: 沉浸式宠物伴侣系统的核数据库表

-- 1. 用户表 (User Table)
CREATE TABLE `t_user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `openid` varchar(64) DEFAULT NULL COMMENT '微信OpenID',
  `phone` varchar(16) DEFAULT NULL COMMENT '手机号',
  `username` varchar(64) DEFAULT NULL COMMENT '用户名',
  `password_hash` varchar(255) DEFAULT NULL COMMENT '密码哈希值(BCrypt)',
  `nickname` varchar(64) DEFAULT NULL COMMENT '昵称',
  `avatar` varchar(512) DEFAULT NULL COMMENT '头像URL',
  `uid` varchar(32) NOT NULL COMMENT '用户唯一ID',
  `status` tinyint(4) DEFAULT '1' COMMENT '状态:1正常,2禁止登录,3禁止发言',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_uid` (`uid`),
  UNIQUE KEY `uk_phone` (`phone`),
  UNIQUE KEY `uk_openid` (`openid`),
  KEY `idx_created_at` (`created_at`)
);

-- 2. 背景表 (Background Resource Table)
CREATE TABLE `t_background` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(64) NOT NULL COMMENT '背景名称',
  `resource_url` varchar(512) NOT NULL COMMENT '资源地址',
  `resource_type` tinyint(4) DEFAULT '1' COMMENT '1:图片 2:视频',
  `visibility` tinyint(4) DEFAULT '1' COMMENT '可见性:1公开,2私有,3VIP',
  `bgm_url` varchar(512) DEFAULT NULL COMMENT '背景音乐URL',
  `is_locked` tinyint(1) DEFAULT '0' COMMENT '是否锁定(需要积分/VIP解锁)',
  `lock_desc` varchar(255) DEFAULT NULL COMMENT '解锁描述',
  `price_points` int(11) DEFAULT '0' COMMENT '解锁所需积分',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_visibility` (`visibility`)
);

-- 3. 宠物(智能体)表 (Pet/Agent Table)
CREATE TABLE `t_pet` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(64) NOT NULL COMMENT '宠物名称',
  `avatar` varchar(512) NOT NULL COMMENT '头像URL',
  `global_prompt` text NOT NULL COMMENT '全局系统提示词',
  `tts_voice_id` varchar(32) DEFAULT NULL COMMENT '腾讯云TTS音色ID',
  `tts_speed` decimal(3,2) DEFAULT '1.00' COMMENT 'TTS语速',
  `tts_volume` decimal(3,2) DEFAULT '1.00' COMMENT 'TTS音量',
  `status` tinyint(4) DEFAULT '1' COMMENT '状态:1启用,2禁用',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
);

-- 4. 背景-宠物关联配置表 (Background-Pet Binding Configuration)
CREATE TABLE `t_background_pet_config` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `background_id` bigint(20) NOT NULL,
  `pet_id` bigint(20) NOT NULL,
  `coordinate_x` decimal(5,2) NOT NULL COMMENT 'X轴百分比 (0-100)',
  `coordinate_y` decimal(5,2) NOT NULL COMMENT 'Y轴百分比 (0-100)',
  `scale` decimal(3,2) DEFAULT '1.00' COMMENT '缩放比例',
  `scene_prompt` text COMMENT '场景专属提示词(追加到全局)',
  `hello_message` varchar(255) COMMENT '场景欢迎语',
  `sort_order` int(11) DEFAULT '0' COMMENT '排序',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_bg_pet` (`background_id`,`pet_id`),
  KEY `idx_background_id` (`background_id`),
  KEY `idx_pet_id` (`pet_id`)
);

-- 5. 用户背景关系表 (User-Background Access Control)
CREATE TABLE `t_user_background_relation` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) NOT NULL,
  `background_id` bigint(20) NOT NULL,
  `is_default` tinyint(1) DEFAULT '0' COMMENT '是否为默认背景',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_bg` (`user_id`,`background_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_background_id` (`background_id`)
);

-- 6. 用户钱包表 (User Quota Management)
CREATE TABLE `t_user_wallet` (
  `user_id` bigint(20) NOT NULL,
  `daily_quota` int(11) DEFAULT '20' COMMENT '每日剩余配额',
  `extra_quota` int(11) DEFAULT '0' COMMENT '永久额外配额',
  `total_consumed` int(11) DEFAULT '0' COMMENT '累计消耗次数',
  `last_reset_time` date DEFAULT NULL COMMENT '上次重置日期',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`user_id`)
);

-- 7. 聊天记录表 (Chat Log with Token Tracking)
CREATE TABLE `t_chat_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) NOT NULL,
  `trace_id` varchar(64) NOT NULL COMMENT '全链路追踪ID',
  `pet_id` bigint(20) NOT NULL,
  `bg_id` bigint(20) NOT NULL,
  `user_msg` text COMMENT '用户消息',
  `ai_msg` text COMMENT 'AI回复消息',
  `tokens_input` int(11) DEFAULT '0' COMMENT '输入token数',
  `tokens_output` int(11) DEFAULT '0' COMMENT '输出token数',
  `duration_ms` int(11) DEFAULT NULL COMMENT '接口响应耗时(毫秒)',
  `conversation_id` varchar(64) NOT NULL COMMENT '会话ID',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_user_time` (`user_id`, `created_at`),
  KEY `idx_trace_id` (`trace_id`),
  KEY `idx_conversation_id` (`conversation_id`),
  KEY `idx_pet_bg_time` (`pet_id`, `bg_id`, `created_at`)
);

-- 8. 系统配置表 (System Configuration)
CREATE TABLE `t_system_config` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `config_key` varchar(64) NOT NULL COMMENT '配置键',
  `config_value` text NOT NULL COMMENT '配置值',
  `description` varchar(255) DEFAULT NULL COMMENT '配置描述',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_config_key` (`config_key`)
);

-- 9. AI模型配置表 (AI Model Configuration)
CREATE TABLE `t_model_config` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(64) NOT NULL COMMENT '模型名称',
  `provider` varchar(32) NOT NULL COMMENT '提供商 (doubao,openai,anthropic等)',
  `model_type` varchar(32) NOT NULL COMMENT '模型类型 (chat,embedding等)',
  `model_key` varchar(128) NOT NULL COMMENT '模型标识符',
  `api_base_url` varchar(255) COMMENT 'API基础URL',
  `api_key` varchar(255) COMMENT 'API密钥',
  `max_tokens` int(11) DEFAULT 4096 COMMENT '最大输出token数',
  `context_length` int(11) DEFAULT 8192 COMMENT '上下文长度',
  `input_price_per_1k` decimal(10,6) DEFAULT 0.000000 COMMENT '输入token价格(每千token)',
  `output_price_per_1k` decimal(10,6) DEFAULT 0.000000 COMMENT '输出token价格(每千token)',
  `temperature` decimal(3,2) DEFAULT 0.7 COMMENT '默认温度参数',
  `top_p` decimal(3,2) DEFAULT 1.00 COMMENT 'Top-p采样参数',
  `enabled` tinyint(1) DEFAULT 1 COMMENT '是否启用',
  `is_default` tinyint(1) DEFAULT 0 COMMENT '是否为默认模型',
  `sort_order` int(11) DEFAULT 0 COMMENT '排序',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_model_key` (`model_key`),
  KEY `idx_provider` (`provider`),
  KEY `idx_enabled` (`enabled`),
  KEY `idx_is_default` (`is_default`)
);

-- 10. 用户会话表 (User Sessions)
CREATE TABLE `t_user_session` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) NOT NULL,
  `session_token` varchar(128) NOT NULL COMMENT 'JWT Token',
  `refresh_token` varchar(128) NOT NULL COMMENT '刷新Token',
  `expires_at` datetime NOT NULL COMMENT '过期时间',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_session_token` (`session_token`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_expires_at` (`expires_at`)
);

-- 插入初始系统配置
INSERT INTO `t_system_config` (`config_key`, `config_value`, `description`) VALUES
('default_background_id', '1', '新用户默认背景ID'),
('daily_quota_initial', '20', '新用户初始每日配额'),
('extra_quota_gift', '0', '新用户赠送额外配额'),
('asr_secret_id', '', '腾讯云ASR SecretId'),
('asr_secret_key', '', '腾讯云ASR SecretKey'),
('tts_secret_id', '', '腾讯云TTS SecretId'),
('tts_secret_key', '', '腾讯云TTS SecretKey'),
('llm_api_key', '', '豆包大模型API Key'),
('api_domain', 'https://api.example.com', 'API域名'),
('ws_domain', 'wss://ws.example.com', 'WebSocket域名'),
('cdn_domain', 'https://cdn.example.com', 'CDN资源域名');

-- 插入默认模型配置
INSERT INTO `t_model_config` (`name`, `provider`, `model_type`, `model_key`, `api_base_url`, `api_key`, `max_tokens`, `context_length`, `input_price_per_1k`, `output_price_per_1k`, `temperature`, `top_p`, `enabled`, `is_default`, `sort_order`) VALUES
('豆包-Pro-4k', 'doubao', 'chat', 'doubao-pro-4k', 'https://ark.cn-beijing.volces.com', '', 4000, 8000, 0.0003, 0.0006, 0.7, 1.0, 1, 1, 1),
('豆包-Pro-32k', 'doubao', 'chat', 'doubao-pro-32k', 'https://ark.cn-beijing.volces.com', '', 8000, 32000, 0.0010, 0.0020, 0.7, 1.0, 1, 0, 2);

-- 创建索引优化查询
CREATE INDEX idx_chat_user_bg_time ON t_chat_log(user_id, bg_id, created_at DESC);
CREATE INDEX idx_chat_user_pet_time ON t_chat_log(user_id, pet_id, created_at DESC);