-- 创建用户钱包和鲸币流水表
CREATE TABLE IF NOT EXISTS `user_wallets` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `user_id` bigint NOT NULL COMMENT '关联users.id',
  `balance` int NOT NULL DEFAULT 0 COMMENT '鲸币余额',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_user_wallets_user_id_unique` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户钱包表';

CREATE TABLE IF NOT EXISTS `wallet_transactions` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `user_id` bigint NOT NULL COMMENT '关联users.id',
  `type` varchar(24) NOT NULL COMMENT '流水类型：grant/spend/earn/refund',
  `amount` int NOT NULL COMMENT '变动数量，收入为正，支出为负',
  `balance_after` int NOT NULL COMMENT '变动后余额',
  `reference_type` varchar(50) NOT NULL COMMENT '业务引用类型',
  `reference_id` varchar(100) NOT NULL COMMENT '业务引用ID',
  `note` varchar(255) DEFAULT NULL COMMENT '流水备注',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_wallet_transactions_user_id` (`user_id`),
  KEY `idx_wallet_transactions_reference` (`reference_type`, `reference_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包鲸币流水表';
