-- 激活码（App 内无支付，资金流外部化）
CREATE TABLE code_batches (
  id          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  channel     ENUM('store','tg','line','manual') NOT NULL,
  created_by  VARCHAR(64) NOT NULL,
  note        VARCHAR(255) NULL,
  created_at  DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE codes (
  id            BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  code_hash     CHAR(64) NOT NULL UNIQUE,          -- SHA-256；明文只出现一次
  plan_id       BIGINT UNSIGNED NOT NULL,
  duration_days INT NOT NULL,
  batch_id      BIGINT UNSIGNED NOT NULL,
  status        ENUM('unused','redeemed','void') NOT NULL DEFAULT 'unused',
  redeemed_by   BIGINT UNSIGNED NULL,
  redeemed_at   DATETIME(6) NULL,
  FOREIGN KEY (plan_id) REFERENCES plans(id),
  FOREIGN KEY (batch_id) REFERENCES code_batches(id),
  INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
