// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type {NextApiRequest, NextApiResponse} from "next";
import client from "@/lib/mongodb";

const db = client.db(process.env.MONGODB_DB);

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
	if (req.method === "GET") return GET(req, res);

	res.status(404).json({ok: false});
}

async function GET(req: NextApiRequest, res: NextApiResponse) {
	const {id} = req.query;

	const snapshot = await db.collection("stats").findOne({ id: id as string});
	if (!snapshot) return res.status(404).json({id: id as string});

	res.status(200).json({...snapshot, id: snapshot.id});
}
