import { ForbiddenException, Inject, NotFoundException } from '@nestjs/common';
import { CommandHandler, type ICommandHandler } from '@nestjs/cqrs';
import { REPORT_REPOSITORY, type IReportRepository } from '../../../domain/repositories/report.repository';
import { DeleteReportCommand } from './delete-report.command';

@CommandHandler(DeleteReportCommand)
export class DeleteReportHandler implements ICommandHandler<DeleteReportCommand, void> {
  constructor(
    @Inject(REPORT_REPOSITORY) private readonly reportRepo: IReportRepository,
  ) {}

  async execute(command: DeleteReportCommand): Promise<void> {
    const report = await this.reportRepo.findById(command.reportId);

    if (!report) {
      throw new NotFoundException('Báo cáo không tồn tại.');
    }

    if (report.userId !== command.userId) {
      throw new ForbiddenException('Bạn không có quyền xóa báo cáo này.');
    }

    await this.reportRepo.delete(command.reportId);
  }
}
