/**
 * Notification Cleanup Service
 *
 * Scheduled job to remove expired notifications from the database.
 * Runs daily to clean up notifications older than 30 days.
 */

import { Injectable, Inject } from "@nestjs/common";
import { Cron, CronExpression } from "@nestjs/schedule";
import { Logger } from "nestjs-pino";
import { NotificationService } from "./notifications.service.js";

@Injectable()
export class NotificationCleanupService {
  constructor(
    private readonly notificationService: NotificationService,
    @Inject(Logger) private readonly logger: Logger
  ) {}

  /**
   * Clean up expired notifications daily at 3 AM
   */
  @Cron(CronExpression.EVERY_DAY_AT_3AM)
  async handleCleanup(): Promise<void> {
    this.logger.debug("Starting notification cleanup job");

    try {
      const count = await this.notificationService.cleanupExpired();

      if (count > 0) {
        this.logger.log("Notification cleanup completed", { deletedCount: count });
      }
    } catch (error) {
      this.logger.error("Notification cleanup job failed", {
        error: error instanceof Error ? error.message : String(error),
      });
    }
  }
}
