import { Processor, WorkerHost } from '@nestjs/bullmq';
import { Job } from 'bullmq';
import { PrismaService } from '../../infrastructure/database/prisma.service';
import { QUEUE_AUDIT_LOG } from '../../infrastructure/queue/queue.service';
import { workerOptionsFor } from '../../infrastructure/queue/queue-definitions';

@Processor(QUEUE_AUDIT_LOG, workerOptionsFor(QUEUE_AUDIT_LOG))
export class AuditLogProcessor extends WorkerHost {
  constructor(private readonly prisma: PrismaService) { super(); }

  async process(job: Job) {
    await this.prisma.adminAuditLog.create({
      data: {
        adminUserId: job.data.adminUserId,
        action: job.data.action,
        resourceType: job.data.resourceType,
        resourceId: job.data.resourceId,
        beforeJson: job.data.beforeJson,
        afterJson: job.data.afterJson,
        ip: job.data.ip,
        userAgent: job.data.userAgent,
        riskLevel: job.data.riskLevel,
        reason: job.data.reason,
      },
    });
  }
}
