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

// ------------------------------------------------------------------------------------

export async function getAppointments(
  req: FastifyRequest,
  reply: FastifyReply
) {
  const appointments = await prisma.appointment.findMany({
    take: N_ITEMS_IN_DASHBOARD,
    orderBy: {
      date: "desc",
    },
    where: {
      active: true,
      activationDate: {
        lte: new Date(),
      },
      date: {
        gte: new Date(),
      },
    },
    select: {
      id: true,
      title: true,
      date: true,
      time: true,
      address: true,
      images: true,
    },
  });

  const newAppointments = await Promise.all(
    appointments.map(async (appointment) => {
      const presignedImages = await Promise.all(
        appointment.images.map(async (image) => await getPresignedUrl(image))
      );
      return {
        id: appointment.id,
        title: appointment.title,
        date: appointment.date,
        time: appointment.time,
        address: appointment.address,
        presignedImages,
      };
    })
  );

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

// ------------------------------------------------------------------------------------

export async function getEvents(req: FastifyRequest, reply: FastifyReply) {
  const events = await prisma.event.findMany({
    take: N_ITEMS_IN_DASHBOARD,
    orderBy: {
      date: "desc",
    },
    where: { active: true },
    select: {
      id: true,
      title: true,
      date: true,
      images: true,
    },
  });

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

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

// ------------------------------------------------------------------------------------

export async function getEditions(req: FastifyRequest, reply: FastifyReply) {
  const editions = await prisma.edition.findMany({
    take: N_ITEMS_IN_DASHBOARD,
    orderBy: {
      title: "asc",
    },
    include: {
      category: true,
    },
  });

  const newEditions = await Promise.all(
    editions.map(async (edition) => ({
      id: edition.id,
      title: edition.title,
      categoryId: edition.categoryId,
      category: edition.category,
      presignedDocument: await getPresignedUrl(edition.document),
      presignedImage: edition.image
        ? await getPresignedUrl(edition.image)
        : null,
    }))
  );

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

// ------------------------------------------------------------------------------------

export async function getAlbums(req: FastifyRequest, reply: FastifyReply) {
  const albums = await prisma.album.findMany({
    take: N_ITEMS_IN_DASHBOARD,
    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);
}

// ------------------------------------------------------------------------------------

export async function getVideos(req: FastifyRequest, reply: FastifyReply) {
  const videos = await prisma.video.findMany({
    take: N_ITEMS_IN_DASHBOARD,
    orderBy: {
      date: "desc",
    },
  });

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

// ------------------------------------------------------------------------------------
