// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type {NextApiRequest, NextApiResponse} from "next";
import {getFirestore, doc, getDoc, setDoc} from "firebase/firestore";
import {withIronSessionApiRoute} from "iron-session/next";
import {sessionOptions} from "@/lib/session";
import axios, {AxiosResponse} from "axios";
import {app} from "@/firebase";
import {Stat} from "@/interfaces/user";
import {writingReverseMarking} from "@/utils/score";

interface Body {
	question: string;
	answer: string;
	task: 1 | 2;
	id: string;
}

function delay(ms: number) {
	return new Promise((resolve) => setTimeout(resolve, ms));
}

const db = getFirestore(app);
export default withIronSessionApiRoute(handler, sessionOptions);

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

	res.status(200).json(null);

	console.log("🌱 - Still processing");
	const backendRequest = await evaluate(req.body as Body);
	console.log("🌱 - Process complete");

	const correspondingStat = await getCorrespondingStat(req.body.id, 1);

	const solutions = correspondingStat.solutions.map((x) => ({...x, evaluation: backendRequest.data}));
	await setDoc(
		doc(db, "stats", (req.body as Body).id),
		{
			solutions,
			score: {
				correct: writingReverseMarking[backendRequest.data.overall],
				total: 100,
				missing: 0,
			},
			isDisabled: false,
		},
		{merge: true},
	);
	console.log("🌱 - Updated the DB");
}

async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
	console.log(`🌱 - Try number ${index} - ${id}`);
	const correspondingStat = await getDoc(doc(db, "stats", id));

	if (correspondingStat.exists()) return {...correspondingStat.data(), id} as Stat;
	await delay(3 * 10000);
	return getCorrespondingStat(id, index + 1);
}

async function evaluate(body: Body): Promise<AxiosResponse> {
	const taskNumber = body.task.toString() !== "1" && body.task.toString() !== "2" ? "1" : body.task.toString();

	const backendRequest = await axios.post(`${process.env.BACKEND_URL}/writing_task${taskNumber}`, body as Body, {
		headers: {
			Authorization: `Bearer ${process.env.BACKEND_JWT}`,
		},
	});

	if (typeof backendRequest.data === "string") return evaluate(body);
	return backendRequest;
}
