-- 训练会话相关表创建脚本
-- 用于支持每日训练实例功能

-- 禁用外键检查
SET FOREIGN_KEY_CHECKS = 0;

-- 删除训练会话相关表（如果存在）
DROP TABLE IF EXISTS `t_workout_exercises`;
DROP TABLE IF EXISTS `t_workout_sessions`;

-- 重新启用外键检查
SET FOREIGN_KEY_CHECKS = 1;

-- 创建训练会话表
CREATE TABLE `t_workout_sessions` (
  `id` char(36) NOT NULL COMMENT '训练会话唯一ID',
  `user_id` varchar(255) NOT NULL COMMENT '用户ID',
  `training_plan_id` char(36) NOT NULL COMMENT '关联的训练计划模板',
  `name` varchar(255) NOT NULL COMMENT '训练会话名称',
  `scheduled_date` datetime NOT NULL COMMENT '计划训练日期',
  `started_at` datetime DEFAULT NULL COMMENT '实际开始时间',
  `completed_at` datetime DEFAULT NULL COMMENT '实际结束时间',
  `status` enum('planned','in_progress','completed','skipped') NOT NULL DEFAULT 'planned' COMMENT '训练状态',
  `total_duration_sec` int DEFAULT NULL COMMENT '总时长(秒)',
  `summary` text COMMENT '训练总结/备注',
  `calories_burned` int DEFAULT NULL COMMENT '消耗卡路里(估算)',
  `stats` json DEFAULT NULL COMMENT '训练统计数据',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否已删除',
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_training_plan_id` (`training_plan_id`),
  KEY `idx_scheduled_date` (`scheduled_date`),
  KEY `idx_status` (`status`),
  KEY `idx_deleted` (`deleted`),
  KEY `idx_user_date` (`user_id`, `scheduled_date`, `deleted`),
  CONSTRAINT `fk_workout_sessions_training_plan` FOREIGN KEY (`training_plan_id`) REFERENCES `t_training_plans` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='训练会话表（每日训练实例）';

-- 创建训练会话动作表
CREATE TABLE `t_workout_exercises` (
  `id` char(36) NOT NULL COMMENT '训练动作唯一ID',
  `workout_session_id` char(36) NOT NULL COMMENT '所属训练会话ID',
  `user_id` varchar(255) NOT NULL COMMENT '用户ID',
  `exercise_key` varchar(255) DEFAULT NULL COMMENT '关联的动作key（仅exercise类型）',
  `name` varchar(255) NOT NULL COMMENT '项目名称',
  `planned_sets` int DEFAULT NULL COMMENT '计划组数',
  `completed_sets` int DEFAULT NULL COMMENT '实际完成组数',
  `planned_reps` int DEFAULT NULL COMMENT '计划重复次数',
  `completed_reps` int DEFAULT NULL COMMENT '实际完成重复次数',
  `planned_duration_sec` int DEFAULT NULL COMMENT '计划持续时长(秒)',
  `actual_duration_sec` int DEFAULT NULL COMMENT '实际持续时长(秒)',
  `rest_sec` int DEFAULT NULL COMMENT '休息时长(秒)',
  `note` text COMMENT '备注',
  `item_type` enum('exercise','rest','note') NOT NULL DEFAULT 'exercise' COMMENT '项目类型',
  `status` enum('pending','in_progress','completed','skipped') NOT NULL DEFAULT 'pending' COMMENT '动作状态',
  `sort_order` int NOT NULL COMMENT '排序顺序',
  `started_at` datetime DEFAULT NULL COMMENT '开始时间',
  `completed_at` datetime DEFAULT NULL COMMENT '完成时间',
  `performance_data` json DEFAULT NULL COMMENT '详细执行数据',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否已删除',
  PRIMARY KEY (`id`),
  KEY `idx_workout_session_id` (`workout_session_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_exercise_key` (`exercise_key`),
  KEY `idx_sort_order` (`sort_order`),
  KEY `idx_status` (`status`),
  KEY `idx_deleted` (`deleted`),
  KEY `idx_session_order` (`workout_session_id`, `sort_order`, `deleted`),
  CONSTRAINT `fk_workout_exercises_session` FOREIGN KEY (`workout_session_id`) REFERENCES `t_workout_sessions` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_workout_exercises_exercise` FOREIGN KEY (`exercise_key`) REFERENCES `t_exercises` (`key`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='训练会话动作表（每日训练实例动作）';

-- 为 t_schedule_exercises 表添加注释，澄清其用途
ALTER TABLE `t_schedule_exercises` COMMENT = '训练计划动作表（训练计划模板的动作配置）';

-- 创建一些有用的索引
CREATE INDEX `idx_workout_sessions_user_status` ON `t_workout_sessions` (`user_id`, `status`, `deleted`);
CREATE INDEX `idx_workout_exercises_session_type` ON `t_workout_exercises` (`workout_session_id`, `item_type`, `deleted`);

-- 插入一些示例数据来测试
-- 注意：实际使用时应该通过API来创建数据
