import type { NextApiRequest, NextApiResponse } from "next";

import findValidApiKey from "@calcom/ee/lib/api/findValidApiKey";
import prisma from "@calcom/prisma";

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  const apiKey = req.query.apiKey as string;

  if (!apiKey) {
    return res.status(401).json({ message: "No API key provided" });
  }

  const validKey = await findValidApiKey(apiKey, "zapier");

  if (!validKey) {
    return res.status(401).json({ message: "API key not valid" });
  }

  if (req.method === "GET") {
    try {
      const bookings = await prisma.booking.findMany({
        take: 3,
        where: {
          userId: validKey.userId,
        },
        select: {
          description: true,
          startTime: true,
          endTime: true,
          title: true,
          location: true,
          cancellationReason: true,
          attendees: {
            select: {
              name: true,
              email: true,
              timeZone: true,
            },
          },
        },
      });

      res.status(201).json(bookings);
    } catch (error) {
      console.error(error);
      return res.status(500).json({ message: "Unable to get bookings." });
    }
  }
}
