-- QuitCheckin (V2) schema notes
-- This file documents the minimal DDL related to the HP persistence model (Phase 3 / issue #39).

-- 1) Profile: add persistent HP field (nullable for migration compatibility)
ALTER TABLE `fa_quit_checkin_profile`
  ADD COLUMN `hp_current` INT NULL COMMENT '肺部HP(0~100)' AFTER `reset_rule`;

-- 2) HP change log: each HP delta is recorded for daily aggregation and future analytics
CREATE TABLE IF NOT EXISTS `fa_quit_checkin_hp_change_log` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
  `created_at` DATETIME(3) NOT NULL COMMENT '创建时间',
  `updated_at` DATETIME(3) NOT NULL COMMENT '更新时间',
  `deleted_at` DATETIME(3) NULL COMMENT '删除时间',

  `uid` INT NOT NULL COMMENT '用户ID',
  `change_date` DATE NOT NULL COMMENT '所属自然日',
  `change_at` DATETIME(3) NOT NULL COMMENT '变动时间',

  `delta` INT NOT NULL COMMENT '变动值(可正可负)',
  `hp_before` INT NOT NULL COMMENT '变动前HP',
  `hp_after` INT NOT NULL COMMENT '变动后HP',
  `reason` VARCHAR(64) NOT NULL COMMENT '变动原因(checkin|smoke|relapse|migrate_init...)',

  `source_type` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '来源类型',
  `source_id` BIGINT UNSIGNED NULL COMMENT '来源ID',

  PRIMARY KEY (`id`),
  KEY `idx_quit_hp_uid_date` (`uid`, `change_date`),
  KEY `idx_quit_hp_reason` (`reason`),
  KEY `idx_quit_hp_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='V2-无烟打卡-HP变动日志';

