-- +goose Up
-- +goose StatementBegin

CREATE TABLE IF NOT EXISTS push_channels (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(64) NOT NULL COMMENT '渠道名称',
  type VARCHAR(32) NOT NULL COMMENT '渠道类型：bark / wpush',
  config JSON NOT NULL COMMENT '渠道配置',
  enabled TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='推送渠道配置';

CREATE TABLE IF NOT EXISTS push_rules (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  event VARCHAR(64) NOT NULL COMMENT '事件类型',
  enabled TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
  threshold INT NOT NULL DEFAULT 5 COMMENT '阈值',
  message_template VARCHAR(255) NOT NULL DEFAULT '' COMMENT '消息模板',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_push_rules_event (event)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='推送通知规则';

INSERT INTO push_rules (event, enabled, threshold, message_template) VALUES
('qrcode_low_stock', 1, 5, '企业微信群二维码库存不足（剩余 {{.Count}} 张），请及时补充')
ON DUPLICATE KEY UPDATE threshold = VALUES(threshold);

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin

DROP TABLE IF EXISTS push_rules;
DROP TABLE IF EXISTS push_channels;

-- +goose StatementEnd
