CREATE TABLE IF NOT EXISTS `promo_rule` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '规则名称',
  `type` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '规则类型：new_user / inactive_user / campaign',
  `params` JSON NOT NULL COMMENT '类型专属参数',
  `priority` INT NOT NULL DEFAULT 0 COMMENT '优先级，数值越大越优先匹配',
  `enabled` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
  `start_time` DATETIME DEFAULT NULL COMMENT '生效开始时间',
  `end_time` DATETIME DEFAULT NULL COMMENT '生效结束时间',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` DATETIME DEFAULT NULL COMMENT '软删除时间',
  PRIMARY KEY (`id`),
  KEY `idx_enabled_priority_deleted` (`enabled`, `deleted_at`, `priority` DESC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='促销规则表';

SET @index_exists = (
  SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'promo_rule'
    AND INDEX_NAME = 'idx_enabled_priority'
);

SET @sql = IF(
  @index_exists = 1,
  'ALTER TABLE `promo_rule` DROP INDEX `idx_enabled_priority`',
  'SELECT ''Index idx_enabled_priority does not exist on promo_rule table'''
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @index_exists = (
  SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'promo_rule'
    AND INDEX_NAME = 'idx_deleted_at'
);

SET @sql = IF(
  @index_exists = 1,
  'ALTER TABLE `promo_rule` DROP INDEX `idx_deleted_at`',
  'SELECT ''Index idx_deleted_at does not exist on promo_rule table'''
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @index_exists = (
  SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'promo_rule'
    AND INDEX_NAME = 'idx_enabled_priority_deleted'
);

SET @sql = IF(
  @index_exists = 0,
  'ALTER TABLE `promo_rule` ADD KEY `idx_enabled_priority_deleted` (`enabled`, `deleted_at`, `priority` DESC)',
  'SELECT ''Index idx_enabled_priority_deleted already exists on promo_rule table'''
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

CREATE TABLE IF NOT EXISTS `subscribe_promo` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `subscribe_id` BIGINT UNSIGNED NOT NULL COMMENT '套餐规格 ID',
  `quantity` BIGINT NOT NULL DEFAULT 1 COMMENT '购买数量',
  `promo_rule_id` BIGINT UNSIGNED NOT NULL COMMENT '促销规则 ID',
  `promo_price` BIGINT NOT NULL DEFAULT 0 COMMENT '该规格在此规则下的优惠价（分）',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_subscribe_quantity_rule` (`subscribe_id`, `quantity`, `promo_rule_id`),
  KEY `idx_promo_rule_id` (`promo_rule_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='规格促销价表';

SET @column_exists = (
  SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'subscribe_promo'
    AND COLUMN_NAME = 'quantity'
);

SET @sql = IF(
  @column_exists = 0,
  'ALTER TABLE `subscribe_promo` ADD COLUMN `quantity` BIGINT NOT NULL DEFAULT 1 COMMENT ''购买数量'' AFTER `subscribe_id`',
  'SELECT ''Column quantity already exists in subscribe_promo table'''
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  @column_exists = 1,
  'ALTER TABLE `subscribe_promo` MODIFY COLUMN `quantity` BIGINT NOT NULL DEFAULT 1 COMMENT ''购买数量''',
  'SELECT ''Column quantity does not exist in subscribe_promo table'''
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @index_exists = (
  SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'subscribe_promo'
    AND INDEX_NAME = 'uk_subscribe_rule'
);

SET @sql = IF(
  @index_exists = 1,
  'ALTER TABLE `subscribe_promo` DROP INDEX `uk_subscribe_rule`',
  'SELECT ''Index uk_subscribe_rule does not exist on subscribe_promo table'''
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @index_exists = (
  SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'subscribe_promo'
    AND INDEX_NAME = 'uk_subscribe_qty_rule'
);

SET @sql = IF(
  @index_exists = 1,
  'ALTER TABLE `subscribe_promo` DROP INDEX `uk_subscribe_qty_rule`',
  'SELECT ''Index uk_subscribe_qty_rule does not exist on subscribe_promo table'''
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @index_exists = (
  SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'subscribe_promo'
    AND INDEX_NAME = 'uk_subscribe_quantity_rule'
);

SET @sql = IF(
  @index_exists = 0,
  'ALTER TABLE `subscribe_promo` ADD UNIQUE KEY `uk_subscribe_quantity_rule` (`subscribe_id`, `quantity`, `promo_rule_id`)',
  'SELECT ''Index uk_subscribe_quantity_rule already exists on subscribe_promo table'''
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

CREATE TABLE IF NOT EXISTS `promo_usage` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户 ID',
  `promo_rule_id` BIGINT UNSIGNED NOT NULL COMMENT '使用的规则 ID',
  `subscribe_id` BIGINT UNSIGNED NOT NULL COMMENT '购买的规格 ID',
  `order_no` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '关联订单号',
  `promo_price` BIGINT NOT NULL DEFAULT 0 COMMENT '使用时的促销单价（分）',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_user_rule` (`user_id`, `promo_rule_id`),
  KEY `idx_order_no` (`order_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='促销使用记录表';

SET @column_exists = (
  SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'order'
    AND COLUMN_NAME = 'promo_rule_id'
);

SET @sql = IF(
  @column_exists = 0,
  'ALTER TABLE `order` ADD COLUMN `promo_rule_id` BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT ''促销规则ID, 0=未使用促销'' AFTER `discount`',
  'SELECT ''Column promo_rule_id already exists in order table'''
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @column_exists = (
  SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'order'
    AND COLUMN_NAME = 'promo_discount'
);

SET @sql = IF(
  @column_exists = 0,
  'ALTER TABLE `order` ADD COLUMN `promo_discount` BIGINT NOT NULL DEFAULT 0 COMMENT ''促销优惠金额(分)'' AFTER `promo_rule_id`',
  'SELECT ''Column promo_discount already exists in order table'''
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
