-- 用户活动记录相关表
-- 包括点赞记录和查看历史

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

-- 2. 作品点赞记录表
CREATE TABLE IF NOT EXISTS `eb_work_like` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `user_id` int(11) NOT NULL COMMENT '用户ID',
  `work_id` bigint(20) NOT NULL COMMENT '作品ID',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '点赞时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_work` (`user_id`, `work_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_work_id` (`work_id`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='作品点赞记录表';

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

-- 4. 查看历史记录表
CREATE TABLE IF NOT EXISTS `eb_view_history` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `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 COMMENT '查看时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_target` (`target_type`, `target_id`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='查看历史记录表';

-- 插入一些测试数据（可选）

-- 测试直播间点赞记录
-- INSERT INTO eb_live_room_like (user_id, room_id, create_time)
-- VALUES 
-- (43, '8', '2026-01-03 14:30:00'),
-- (43, '1', '2026-01-03 13:20:00');

-- 测试查看历史记录
-- INSERT INTO eb_view_history (user_id, target_type, target_id, target_title, view_duration, create_time)
-- VALUES 
-- (43, 'room', '8', '火影忍者', 1200, '2026-01-03 14:25:00'),
-- (43, 'room', '1', '11', 300, '2026-01-03 13:15:00'),
-- (43, 'profile', '41', '夏至已至', 60, '2026-01-03 14:19:00');

-- 查询验证
-- SELECT * FROM eb_live_room_like WHERE user_id = 43;
-- SELECT * FROM eb_view_history WHERE user_id = 43;
