import { Injectable } from '@nestjs/common';
import { Photo } from '../domain/Photo';
import { PhotoMapper } from '../mappers/photo.mapper';
import { Shooting } from '../domain/Shooting';
import { PrismaService } from '@/database/services/prisma.service';
import { ShootingMapper } from '../mappers/shooting.mapper';

@Injectable()
export class PhotoRepo {
  constructor(private prisma: PrismaService) {}

  async findById(id: string): Promise<Photo | null> {
    const photo = await this.prisma.photo.findUnique({
      where: {
        id: id,
      },
      include: {
        shooting: true,
      },
    });

    if (!photo) {
      return null;
    }

    return PhotoMapper.toDomain(photo, ShootingMapper.toDomain(photo.shooting));
  }

  async findMany(ids: string[]): Promise<Photo[]> {
    const photos = await this.prisma.photo.findMany({
      where: {
        id: {
          in: ids,
        },
      },
      include: {
        shooting: true,
      },
    });

    return photos.map((photo) =>
      PhotoMapper.toDomain(photo, ShootingMapper.toDomain(photo.shooting)),
    );
  }

  async create(photo: Photo): Promise<Photo> {
    const photoData = PhotoMapper.toPrisma(photo);

    const createdPhoto = await this.prisma.photo.create({
      data: photoData,
    });

    return await this.findById(createdPhoto.id);
  }

  async createMany(photos: Photo[]): Promise<Photo[]> {
    const photosData = photos.map(PhotoMapper.toPrisma);

    const createdPhotos = await this.prisma.photo.createMany({
      data: photosData,
    });

    const createdPhotoIds = await this.prisma.photo.findMany({
      where: {
        shootingId: photos[0].shootingId,
      },
      orderBy: {
        createdAt: 'desc',
      },
      take: photos.length,
    });

    return createdPhotoIds.map((photo) => PhotoMapper.toDomain(photo));
  }

  async deleteMany(ids: string[]): Promise<void> {
    await this.prisma.photo.deleteMany({
      where: {
        id: {
          in: ids,
        },
      },
    });
  }

  async update(photo: Photo): Promise<Photo> {
    const photoData = PhotoMapper.toPrisma(photo);

    const updatedPhoto = await this.prisma.photo.update({
      where: {
        id: photo.id,
      },
      data: photoData,
    });

    return PhotoMapper.toDomain(updatedPhoto);
  }

  async delete(id: string): Promise<void> {
    await this.prisma.photo.delete({
      where: {
        id: id,
      },
    });
  }

  async findByShootingId(shootingId: string): Promise<Photo[]> {
    const photos = await this.prisma.photo.findMany({
      where: {
        shootingId: shootingId,
      },
      include: {
        shooting: true,
      },
    });

    return photos.map((photo) =>
      PhotoMapper.toDomain(photo, ShootingMapper.toDomain(photo.shooting)),
    );
  }

  async deleteByShootingId(shootingId: string) {
    const photosToDelete = await this.findByShootingId(shootingId);

    await this.prisma.photo.deleteMany({
      where: {
        shootingId: shootingId,
      },
    });

    return photosToDelete.map((photo) => PhotoMapper.toDomain(photo));
  }
}
