import { Injectable, Logger } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { IdempotencyStoreService } from './idempotency-store.service';

/** Limpieza diaria de claves de idempotencia móvil vencidas (TTL configurable). */
@Injectable()
export class IdempotencyCleanupService {
  private readonly logger = new Logger(IdempotencyCleanupService.name);

  constructor(private readonly store: IdempotencyStoreService) {}

  @Cron('0 3 * * *')
  async limpiarVencidas(): Promise<void> {
    try {
      const borradas = await this.store.cleanupExpired();
      if (borradas > 0) {
        this.logger.log(`Idempotencia móvil: ${borradas} claves vencidas eliminadas`);
      }
    } catch (err) {
      this.logger.error('Error limpiando idempotencia móvil', err as Error);
    }
  }
}
