// services/alert-cron.service.ts
import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { AlertService } from '../alert.service';

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

  constructor(private readonly alertService: AlertService) {}

  // Run every hour to fetch new attack data
  @Cron(CronExpression.EVERY_MINUTE)
  async handleHourlyDataCollection() {
    this.logger.log('Starting hourly attack data collection...');

    try {
      await this.alertService.fetchExternalAlerts({ page: 1, limit: 10 });
      this.logger.log('Hourly attack data collection completed successfully');
    } catch (error) {
      this.logger.error(`Error during hourly attack data collection: ${error.message}`);
    }
  }

  // Run every 5 minutes to check critical sources (like Nepal CERT)
  @Cron(CronExpression.EVERY_5_MINUTES)
  async handleCriticalSourcesCheck() {
    this.logger.log('Checking critical sources for new alerts...');

    try {
      // You can implement a more focused check for critical sources here
      // For example, only check Nepal CERT and other high-priority sources
      // This is a placeholder - you would need to implement this method in AlertService
      // await this.alertService.checkCriticalSources();
      this.logger.log('Critical sources check completed successfully');
    } catch (error) {
      this.logger.error(`Error during critical sources check: ${error.message}`);
    }
  }

  // Run daily to clean up old unverified alerts
  @Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
  async handleDailyCleanup() {
    this.logger.log('Starting daily cleanup of old unverified alerts...');

    try {
      // This is a placeholder - you would need to implement this method in AlertService
      // await this.alertService.cleanupOldUnverifiedAlerts(30); // Cleanup alerts older than 30 days
      this.logger.log('Daily cleanup completed successfully');
    } catch (error) {
      this.logger.error(`Error during daily cleanup: ${error.message}`);
    }
  }
}
