import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { RewardApplicationService } from '../services/reward-application.service';

@Injectable()
export class RewardExpirationScheduler {
  private readonly logger = new Logger(RewardExpirationScheduler.name);

  constructor(
    private readonly rewardService: RewardApplicationService,
  ) {}

  /**
   * 每小时检查过期的待领取奖励
   */
  @Cron(CronExpression.EVERY_HOUR)
  async handleExpiredRewards() {
    this.logger.log('开始检查过期奖励...');

    try {
      const result = await this.rewardService.expireOverdueRewards();
      this.logger.log(`过期奖励处理完成：${result.expiredCount}笔，共${result.totalUsdtExpired} USDT`);
    } catch (error) {
      this.logger.error('处理过期奖励时发生错误:', error);
    }
  }
}
