-- 推送令牌表
CREATE TABLE t_user_push_tokens (
  id VARCHAR(36) PRIMARY KEY DEFAULT (UUID()),
  user_id VARCHAR(255) NOT NULL COMMENT '用户ID',
  device_token VARCHAR(255) NOT NULL COMMENT '设备推送令牌',
  device_type ENUM('IOS', 'ANDROID') NOT NULL DEFAULT 'IOS' COMMENT '设备类型',
  app_version VARCHAR(50) NULL COMMENT '应用版本',
  os_version VARCHAR(50) NULL COMMENT '操作系统版本',
  device_name VARCHAR(255) NULL COMMENT '设备名称',
  is_active BOOLEAN DEFAULT TRUE COMMENT '是否激活',
  last_used_at DATETIME NULL COMMENT '最后使用时间',
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  INDEX idx_user_id (user_id),
  INDEX idx_device_token (device_token),
  INDEX idx_user_device (user_id, device_token),
  UNIQUE KEY uk_user_device_token (user_id, device_token)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户推送令牌表';

-- 推送消息表
CREATE TABLE t_push_messages (
  id VARCHAR(36) PRIMARY KEY DEFAULT (UUID()),
  user_id VARCHAR(255) NOT NULL COMMENT '用户ID',
  device_token VARCHAR(255) NOT NULL COMMENT '设备推送令牌',
  message_type VARCHAR(50) NOT NULL COMMENT '消息类型',
  title VARCHAR(255) NULL COMMENT '推送标题',
  body TEXT NULL COMMENT '推送内容',
  payload JSON NULL COMMENT '自定义负载数据',
  push_type ENUM('ALERT', 'BACKGROUND', 'VOIP', 'LIVEACTIVITY') NOT NULL DEFAULT 'ALERT' COMMENT '推送类型',
  priority TINYINT NOT NULL DEFAULT 10 COMMENT '优先级',
  expiry DATETIME NULL COMMENT '过期时间',
  collapse_id VARCHAR(64) NULL COMMENT '折叠ID',
  status ENUM('PENDING', 'SENT', 'FAILED', 'EXPIRED') NOT NULL DEFAULT 'PENDING' COMMENT '推送状态',
  apns_response JSON NULL COMMENT 'APNs响应数据',
  error_message TEXT NULL COMMENT '错误信息',
  sent_at DATETIME NULL COMMENT '发送时间',
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  INDEX idx_user_id (user_id),
  INDEX idx_status (status),
  INDEX idx_created_at (created_at),
  INDEX idx_message_type (message_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='推送消息表';

-- 推送模板表
CREATE TABLE t_push_templates (
  id VARCHAR(36) PRIMARY KEY DEFAULT (UUID()),
  template_key VARCHAR(100) NOT NULL UNIQUE COMMENT '模板键',
  title VARCHAR(255) NOT NULL COMMENT '模板标题',
  body TEXT NOT NULL COMMENT '模板内容',
  payload_template JSON NULL COMMENT '负载模板',
  push_type ENUM('ALERT', 'BACKGROUND', 'VOIP', 'LIVEACTIVITY') NOT NULL DEFAULT 'ALERT' COMMENT '推送类型',
  priority TINYINT NOT NULL DEFAULT 10 COMMENT '优先级',
  is_active BOOLEAN DEFAULT TRUE COMMENT '是否激活',
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  INDEX idx_template_key (template_key),
  INDEX idx_is_active (is_active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='推送模板表';

-- 插入默认推送模板
INSERT INTO t_push_templates (template_key, title, body, payload_template, push_type, priority, is_active) VALUES
('training_reminder', '训练提醒', '您好{{userName}}，您今天的{{trainingName}}训练还未完成，快来打卡吧！', '{"type": "training_reminder", "trainingId": "{{trainingId}}"}', 'ALERT', 10, TRUE),
('diet_record_reminder', '饮食记录提醒', '您好{{userName}}，您还没有记录今天的饮食，记得及时记录哦！', '{"type": "diet_record_reminder"}', 'ALERT', 8, TRUE),
('challenge_progress', '挑战进度', '恭喜您！您已完成{{challengeName}}挑战的{{progress}}%，继续加油！', '{"type": "challenge_progress", "challengeId": "{{challengeId}}"}', 'ALERT', 9, TRUE),
('membership_expiring', '会员到期提醒', '您好{{userName}}，您的会员将在{{days}}天后到期，请及时续费以免影响使用。', '{"type": "membership_expiring", "days": {{days}}}', 'ALERT', 10, TRUE),
('membership_expired', '会员已到期', '您好{{userName}}，您的会员已到期，请续费以继续享受会员服务。', '{"type": "membership_expired"}', 'ALERT', 10, TRUE),
('achievement_unlocked', '成就解锁', '恭喜您解锁了"{{achievementName}}"成就！', '{"type": "achievement_unlocked", "achievementId": "{{achievementId}}"}', 'ALERT', 9, TRUE),
('workout_completed', '训练完成', '太棒了！您已完成{{workoutName}}训练，消耗了{{calories}}卡路里。', '{"type": "workout_completed", "workoutId": "{{workoutId}}", "calories": {{calories}}}', 'ALERT', 8, TRUE);