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

export default withIronSessionApiRoute(handler, sessionOptions);

async function handler(req: NextApiRequest, res: NextApiResponse) {
	if (req.method === "POST") return post(req, res);

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

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

	const body = req.body;
	const params = new URLSearchParams();

	Object.keys(body).forEach((key) => params.append(key, body[key]));
	const result = await axios.get(`${process.env.BACKEND_URL}/custom_level?${params.toString()}`, {
		headers: {Authorization: `Bearer ${process.env.BACKEND_JWT}`},
	});

	res.status(200).json(result.data);
}
