// 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";

interface Body {
	userId: string;
	sessionId: string;
	question: string;
	answer: string;
	exerciseId: string;
	task: 1 | 2;
}

export default withIronSessionApiRoute(handler, sessionOptions);


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

	const { task, ...body}  = req.body as Body;
	const taskNumber = task.toString() !== "1" && task.toString() !== "2" ? "1" : task.toString();

	await axios.post(`${process.env.BACKEND_URL}/grade/writing/${taskNumber}`, body, {
		headers: {
			Authorization: `Bearer ${process.env.BACKEND_JWT}`,
		},
	});
	res.status(200).json({ok: true});
}
