-- 创建目标表
CREATE TABLE IF NOT EXISTS `t_goals` (
  `id` char(36) NOT NULL COMMENT '主键ID',
  `user_id` varchar(255) NOT NULL COMMENT '用户ID',
  `title` varchar(255) NOT NULL COMMENT '目标标题',
  `description` text COMMENT '目标描述',
  `repeat_type` enum('daily','weekly','monthly','custom') NOT NULL DEFAULT 'daily' COMMENT '重复周期类型：daily-每日，weekly-每周，monthly-每月，custom-自定义',
  `frequency` int NOT NULL DEFAULT 1 COMMENT '频率（每天/每周/每月多少次）',
  `custom_repeat_rule` json DEFAULT NULL COMMENT '自定义重复规则（如每周几）',
  `start_date` date NOT NULL COMMENT '目标开始日期',
  `end_date` date DEFAULT NULL COMMENT '目标结束日期',
  `status` enum('active','paused','completed','cancelled') NOT NULL DEFAULT 'active' COMMENT '目标状态：active-激活，paused-暂停，completed-已完成，cancelled-已取消',
  `completed_count` int NOT NULL DEFAULT 0 COMMENT '已完成次数',
  `target_count` int DEFAULT NULL COMMENT '目标总次数（null表示无限制）',
  `category` varchar(100) DEFAULT NULL COMMENT '目标分类标签',
  `priority` int NOT NULL DEFAULT 0 COMMENT '优先级（数字越大优先级越高）',
  `has_reminder` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否提醒',
  `reminder_time` time DEFAULT NULL COMMENT '提醒时间',
  `reminder_settings` 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_status` (`status`),
  KEY `idx_repeat_type` (`repeat_type`),
  KEY `idx_category` (`category`),
  KEY `idx_start_date` (`start_date`),
  KEY `idx_deleted` (`deleted`),
  KEY `idx_user_status` (`user_id`, `status`, `deleted`),
  CONSTRAINT `chk_frequency` CHECK (`frequency` > 0 AND `frequency` <= 100),
  CONSTRAINT `chk_priority` CHECK (`priority` >= 0 AND `priority` <= 10),
  CONSTRAINT `chk_target_count` CHECK (`target_count` IS NULL OR `target_count` > 0)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户目标表';

-- 创建目标完成记录表
CREATE TABLE IF NOT EXISTS `t_goal_completions` (
  `id` char(36) NOT NULL COMMENT '主键ID',
  `goal_id` char(36) NOT NULL COMMENT '目标ID',
  `user_id` varchar(255) NOT NULL COMMENT '用户ID',
  `completed_at` datetime NOT NULL COMMENT '完成日期',
  `completion_count` int NOT NULL DEFAULT 1 COMMENT '完成次数',
  `notes` text COMMENT '完成备注',
  `metadata` 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_goal_id` (`goal_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_completed_at` (`completed_at`),
  KEY `idx_deleted` (`deleted`),
  KEY `idx_goal_completed` (`goal_id`, `completed_at`, `deleted`),
  CONSTRAINT `fk_goal_completions_goal` FOREIGN KEY (`goal_id`) REFERENCES `t_goals` (`id`) ON DELETE CASCADE,
  CONSTRAINT `chk_completion_count` CHECK (`completion_count` > 0)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='目标完成记录表';

-- 创建额外的复合索引以优化查询性能
CREATE INDEX IF NOT EXISTS `idx_goals_user_date` ON `t_goals` (`user_id`, `start_date`, `deleted`);
CREATE INDEX IF NOT EXISTS `idx_goal_completions_user_date` ON `t_goal_completions` (`user_id`, `completed_at`, `deleted`);