// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from "next";
import { app } from "@/firebase";
import {
  getFirestore,
  getDocs,
  collection,
} from "firebase/firestore";
import { withIronSessionApiRoute } from "iron-session/next";
import { sessionOptions } from "@/lib/session";

const db = getFirestore(app);

export default withIronSessionApiRoute(handler, sessionOptions);

async function get(req: NextApiRequest, res: NextApiResponse) {
  const payments = await getDocs(collection(db, "paypalpayments"));

  const data = payments.docs.map((doc) => doc.data());
  res.status(200).json(data);
}

async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (!req.session.user) {
    res.status(401).json({ ok: false });
    return;
  }

  if (req.method === "GET") await get(req, res);
}
