import { Processor, WorkerHost } from '@nestjs/bullmq';
import { Job } from 'bullmq';
import { Logger } from '@nestjs/common';
import { QUEUE_FILE_CLEANUP } from '../../infrastructure/queue/queue.service';
import { workerOptionsFor } from '../../infrastructure/queue/queue-definitions';
import { CosStorageProvider } from '../../infrastructure/storage/cos-storage.provider';

@Processor(QUEUE_FILE_CLEANUP, workerOptionsFor(QUEUE_FILE_CLEANUP))
export class FileCleanupProcessor extends WorkerHost {
  private readonly logger = new Logger(FileCleanupProcessor.name);

  constructor(private readonly cos: CosStorageProvider) { super(); }

  async process(job: Job) {
    const { objectKey, bucket, region } = job.data;
    if (!objectKey) return;
    try {
      await this.cos.deleteObject(objectKey);
      this.logger.log(`Cleaned up COS: ${objectKey}`);
    } catch (err: any) {
      this.logger.warn(`Cleanup failed for ${objectKey}: ${err.message}`);
      throw err; // BullMQ will retry
    }
  }
}
