// 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, {AxiosResponse} from "axios";
import formidable from "formidable-serverless";
import {ref, uploadBytes} from "firebase/storage";
import fs from "fs";
import {storage} from "@/firebase";
import client from "@/lib/mongodb";
import {Stat} from "@/interfaces/user";
import {speakingReverseMarking} from "@/utils/score";

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

export default withIronSessionApiRoute(handler, sessionOptions);

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

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

	const form = formidable({keepExtensions: true});
	await form.parse(req, async (err: any, fields: any, files: any) => {
		if (err) console.log(err);

		const uploadingAudios = await Promise.all(
			Object.keys(files).map(async (fileID: string) => {
				const audioFile = files[fileID];
				const questionID = fileID.replace("answer_", "question_");

				const audioFileRef = ref(storage, `speaking_recordings/${(audioFile as any).path.replace("upload_", "")}`);

				const binary = fs.readFileSync((audioFile as any).path).buffer;
				const snapshot = await uploadBytes(audioFileRef, binary);

				fs.rmSync((audioFile as any).path);

				return {question: fields[questionID], answer: snapshot.metadata.fullPath};
			}),
		);

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

		console.log("🌱 - Still processing");
		const backendRequest = await evaluate({answers: uploadingAudios}, fields.variant);
		console.log("🌱 - Process complete");

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

		const solutions = correspondingStat.solutions.map((x) => ({...x, evaluation: backendRequest.data, solution: uploadingAudios}));
		await db.collection("stats").updateOne(
			{ id: fields.id },
			{
			  $set: {
				id: fields.id,
				solutions,
				score: {
				  correct: speakingReverseMarking[backendRequest.data.overall || 0] || 0,
				  missing: 0,
				  total: 100,
				},
				isDisabled: false
			  }
			},
			{ upsert: true }
		);
		console.log("🌱 - Updated the DB");
	});
}

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

	if (correspondingStat) return correspondingStat;
	await delay(3 * 10000);
	return getCorrespondingStat(id, index + 1);
}

async function evaluate(body: {answers: object[]}, variant?: "initial" | "final"): Promise<AxiosResponse> {
	const backendRequest = await axios.post(`${process.env.BACKEND_URL}/speaking_task_${variant === "initial" ? "1" : "3"}`, body, {
		headers: {
			Authorization: `Bearer ${process.env.BACKEND_JWT}`,
		},
	});

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

export const config = {
	api: {
		bodyParser: false,
	},
};
