import { Controller, Delete, Param } from '@nestjs/common';
import { ShootingRepo } from '../../repos/shooting.repo';
import { PhotoRepo } from '../../repos/photo.repo';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { assertRessourceExists } from '@/core/functions/assert';

@Controller('shootings')
export class DeleteShooting {
  constructor(
    private shootingRepo: ShootingRepo,
    private photoRepo: PhotoRepo,
    private eventEmitter: EventEmitter2,
  ) {}

  @Delete(':id')
  async execute(@Param('id') id: string): Promise<{}> {
    const shooting = await this.shootingRepo.findById(id);

    assertRessourceExists(shooting);

    const photosDeleted = await this.photoRepo.deleteByShootingId(id);

    await this.shootingRepo.delete(id);

    for (const photo of photosDeleted) {
      this.eventEmitter.emit('photo.deleted', photo);
    }

    return {};
  }
}
