import { FastifyReply, FastifyRequest } from "fastify";
import { getPresignedUrl } from "../../utils/minio";
import prisma from "../../utils/prisma";

export async function getAlbums(req: FastifyRequest, reply: FastifyReply) {
  const albums = await prisma.album.findMany({
    orderBy: {
      date: "desc",
    },
  });

  const newAlbums = await Promise.all(
    albums.map(async (album) => {
      const presignedImages = await Promise.all(
        album.images.map(async (image) => await getPresignedUrl(image))
      );
      return {
        id: album.id,
        title: album.title,
        date: album.date,
        presignedImages,
      };
    })
  );

  return reply.code(200).send(newAlbums);
}
