-- +goose Up

ALTER TABLE users
  ADD COLUMN renter_growth_points BIGINT NOT NULL DEFAULT 0 COMMENT '租客成长积分' AFTER deposit_free_quota_cent,
  ADD COLUMN renter_growth_level VARCHAR(32) NOT NULL DEFAULT 'normal' COMMENT '租客成长等级: normal普通/platinum铂金/diamond钻石/peak巅峰' AFTER renter_growth_points,
  ADD KEY idx_users_renter_growth_level (renter_growth_level, renter_growth_points);

ALTER TABLE rental_orders
  ADD COLUMN rent_original_amount_cent BIGINT NOT NULL DEFAULT 0 COMMENT '租客下单原始租金(分)' AFTER platform_fee_cent,
  ADD COLUMN rent_discount_amount_cent BIGINT NOT NULL DEFAULT 0 COMMENT '租客成长等级优惠金额(分)' AFTER rent_original_amount_cent,
  ADD COLUMN renter_growth_level VARCHAR(32) NOT NULL DEFAULT 'normal' COMMENT '下单时租客成长等级快照' AFTER rent_discount_amount_cent,
  ADD COLUMN renter_growth_level_name VARCHAR(32) NOT NULL DEFAULT '普通' COMMENT '下单时租客成长等级名称快照' AFTER renter_growth_level,
  ADD COLUMN renter_discount_bps INT NOT NULL DEFAULT 10000 COMMENT '下单时租客等级折扣，10000=无折扣' AFTER renter_growth_level_name,
  ADD COLUMN growth_points_awarded BIGINT NOT NULL DEFAULT 0 COMMENT '订单完成后已发放成长积分' AFTER renter_discount_bps,
  ADD COLUMN growth_points_awarded_at DATETIME NULL COMMENT '成长积分发放时间' AFTER growth_points_awarded,
  ADD KEY idx_rental_orders_growth_awarded (growth_points_awarded_at, renter_id);

UPDATE rental_orders
SET rent_original_amount_cent = rent_amount_cent
WHERE rent_original_amount_cent = 0;

CREATE TABLE renter_growth_ledger (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
  order_id BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '租号订单ID',
  points BIGINT NOT NULL DEFAULT 0 COMMENT '变动积分',
  before_points BIGINT NOT NULL DEFAULT 0 COMMENT '变动前积分',
  after_points BIGINT NOT NULL DEFAULT 0 COMMENT '变动后积分',
  before_level VARCHAR(32) NOT NULL DEFAULT 'normal' COMMENT '变动前等级',
  after_level VARCHAR(32) NOT NULL DEFAULT 'normal' COMMENT '变动后等级',
  source VARCHAR(32) NOT NULL DEFAULT 'order_completed' COMMENT '来源',
  remark VARCHAR(255) NOT NULL DEFAULT '' COMMENT '备注',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_renter_growth_order_source (order_id, source),
  KEY idx_renter_growth_ledger_user (user_id),
  KEY idx_renter_growth_ledger_order (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='租客成长积分流水';

INSERT INTO system_configs (`key`, `value`, `description`, `created_at`, `updated_at`)
VALUES (
  'renter.growth_level_rules',
  '{"enabled":true,"points_per_yuan":1,"levels":[{"code":"normal","name":"普通","min_points":0,"discount_bps":10000},{"code":"platinum","name":"铂金","min_points":300,"discount_bps":9900},{"code":"diamond","name":"钻石","min_points":1000,"discount_bps":9800},{"code":"peak","name":"巅峰","min_points":5000,"discount_bps":9500}]}',
  '租客成长等级规则 JSON（积分门槛/下单折扣）',
  NOW(),
  NOW()
)
ON DUPLICATE KEY UPDATE description = VALUES(description);

-- +goose Down

DELETE FROM system_configs WHERE `key` = 'renter.growth_level_rules';

DROP TABLE renter_growth_ledger;

ALTER TABLE rental_orders
  DROP KEY idx_rental_orders_growth_awarded,
  DROP COLUMN growth_points_awarded_at,
  DROP COLUMN growth_points_awarded,
  DROP COLUMN renter_discount_bps,
  DROP COLUMN renter_growth_level_name,
  DROP COLUMN renter_growth_level,
  DROP COLUMN rent_discount_amount_cent,
  DROP COLUMN rent_original_amount_cent;

ALTER TABLE users
  DROP KEY idx_users_renter_growth_level,
  DROP COLUMN renter_growth_level,
  DROP COLUMN renter_growth_points;
