-- =====================================================
-- 完整的用户活动数据初始化脚本
-- 确保移动端"我的记录"功能正常显示
-- =====================================================

-- 1. 确保所有必要的表存在
-- =====================================================

-- 观看历史表
CREATE TABLE IF NOT EXISTS `eb_view_history` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL COMMENT '用户ID',
  `target_type` varchar(20) NOT NULL COMMENT '目标类型：room-直播间, work-作品, profile-用户主页',
  `target_id` varchar(50) NOT NULL COMMENT '目标ID',
  `target_title` varchar(255) DEFAULT NULL COMMENT '目标标题',
  `view_duration` int(11) DEFAULT 0 COMMENT '观看时长（秒）',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_target` (`target_type`, `target_id`),
  KEY `idx_update_time` (`update_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='观看历史记录表';

-- 直播间点赞表
CREATE TABLE IF NOT EXISTS `eb_live_room_like` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL COMMENT '用户ID',
  `room_id` varchar(50) NOT NULL COMMENT '直播间ID',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_room` (`user_id`, `room_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_room_id` (`room_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='直播间点赞记录表';

-- 心愿点赞表
CREATE TABLE IF NOT EXISTS `eb_wish_like` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL COMMENT '用户ID',
  `wish_id` bigint(20) NOT NULL COMMENT '心愿ID',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_wish` (`user_id`, `wish_id`),
  KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='心愿点赞记录表';

-- 搜索历史表
CREATE TABLE IF NOT EXISTS `eb_search_history` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL COMMENT '用户ID',
  `keyword` varchar(100) NOT NULL COMMENT '搜索关键词',
  `search_type` varchar(20) DEFAULT 'all' COMMENT '搜索类型',
  `is_deleted` tinyint(1) DEFAULT 0 COMMENT '是否删除',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_keyword` (`keyword`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='搜索历史记录表';

-- 2. 为用户121（道玄）添加完整测试数据
-- =====================================================

SET @USER_ID = 121;

-- 清除旧数据（可选）
-- DELETE FROM eb_view_history WHERE user_id = @USER_ID;
-- DELETE FROM eb_live_room_like WHERE user_id = @USER_ID;

-- 添加观看历史
INSERT IGNORE INTO eb_view_history (user_id, target_type, target_id, target_title, view_duration, create_time, update_time) VALUES
(@USER_ID, 'room', '1', '欢乐游戏直播', 1800, NOW() - INTERVAL 1 HOUR, NOW() - INTERVAL 1 HOUR),
(@USER_ID, 'room', '2', '音乐分享会', 2400, NOW() - INTERVAL 2 HOUR, NOW() - INTERVAL 2 HOUR),
(@USER_ID, 'room', '3', '户外探险', 1200, NOW() - INTERVAL 3 HOUR, NOW() - INTERVAL 3 HOUR),
(@USER_ID, 'room', '4', '美食制作教程', 3600, NOW() - INTERVAL 4 HOUR, NOW() - INTERVAL 4 HOUR),
(@USER_ID, 'room', '5', '编程技术分享', 5400, NOW() - INTERVAL 5 HOUR, NOW() - INTERVAL 5 HOUR);

-- 添加直播间点赞
INSERT IGNORE INTO eb_live_room_like (user_id, room_id, create_time) VALUES
(@USER_ID, '1', NOW() - INTERVAL 1 HOUR),
(@USER_ID, '2', NOW() - INTERVAL 2 HOUR),
(@USER_ID, '3', NOW() - INTERVAL 3 HOUR);

-- 3. 验证数据
-- =====================================================

SELECT '=== 用户121的观看历史 ===' as info;
SELECT id, user_id, target_type, target_id, target_title, view_duration, 
       DATE_FORMAT(update_time, '%Y-%m-%d %H:%i:%s') as update_time
FROM eb_view_history 
WHERE user_id = @USER_ID 
ORDER BY update_time DESC;

SELECT '=== 用户121的直播间点赞 ===' as info;
SELECT rl.id, rl.user_id, rl.room_id, lr.title as room_title,
       DATE_FORMAT(rl.create_time, '%Y-%m-%d %H:%i:%s') as create_time
FROM eb_live_room_like rl
LEFT JOIN eb_live_room lr ON rl.room_id = lr.id
WHERE rl.user_id = @USER_ID;

SELECT '=== 用户121的关注记录 ===' as info;
SELECT fr.id, fr.follower_id, fr.followed_id, fr.followed_nickname, fr.follow_status,
       DATE_FORMAT(fr.create_time, '%Y-%m-%d %H:%i:%s') as create_time
FROM eb_follow_record fr
WHERE fr.follower_id = @USER_ID AND (fr.follow_status = 1 OR fr.follow_status = '关注');

SELECT '=== 用户121的收藏作品 ===' as info;
SELECT wr.id, wr.uid, wr.works_id, w.title,
       DATE_FORMAT(wr.create_time, '%Y-%m-%d %H:%i:%s') as create_time
FROM eb_works_relation wr
LEFT JOIN eb_works w ON wr.works_id = w.id
WHERE wr.uid = @USER_ID AND wr.type = 2;

SELECT '=== 数据汇总 ===' as info;
SELECT 
    (SELECT COUNT(*) FROM eb_view_history WHERE user_id = @USER_ID) as 观看历史数,
    (SELECT COUNT(*) FROM eb_live_room_like WHERE user_id = @USER_ID) as 直播间点赞数,
    (SELECT COUNT(*) FROM eb_follow_record WHERE follower_id = @USER_ID AND (follow_status = 1 OR follow_status = '关注')) as 关注数,
    (SELECT COUNT(*) FROM eb_works_relation WHERE uid = @USER_ID AND type = 2) as 收藏作品数;
