import { Injectable } from '@nestjs/common';
import { RewardLedgerEntry } from '../aggregates/reward-ledger-entry/reward-ledger-entry.aggregate';

@Injectable()
export class RewardExpirationService {
  /**
   * 检查并过期所有到期的待领取奖励
   */
  expireOverdueRewards(pendingRewards: RewardLedgerEntry[]): RewardLedgerEntry[] {
    const expiredRewards: RewardLedgerEntry[] = [];

    for (const reward of pendingRewards) {
      if (reward.isExpiredNow()) {
        reward.expire();
        expiredRewards.push(reward);
      }
    }

    return expiredRewards;
  }

  /**
   * 检查用户的待领取奖励状态
   */
  checkUserPendingRewards(
    pendingRewards: RewardLedgerEntry[],
  ): {
    expired: RewardLedgerEntry[];
    stillPending: RewardLedgerEntry[];
  } {
    const expired: RewardLedgerEntry[] = [];
    const stillPending: RewardLedgerEntry[] = [];

    for (const reward of pendingRewards) {
      if (reward.isExpiredNow()) {
        reward.expire();
        expired.push(reward);
      } else {
        stillPending.push(reward);
      }
    }

    return { expired, stillPending };
  }
}
