// 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 {getDownloadURL, ref, uploadBytes} from "firebase/storage";
import fs from "fs";
import {app, storage} from "@/firebase";
import {doc, getDoc, getFirestore, setDoc} from "firebase/firestore";
import {Stat} from "@/interfaces/user";
import {speakingReverseMarking} from "@/utils/score";

const db = getFirestore(app);
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 audioFile = files.audio;
		const audioFileRef = ref(storage, `speaking_recordings/${fields.id}.wav`);
		const task = parseInt(fields.task.toString());

		const binary = fs.readFileSync((audioFile as any).path).buffer;
		const snapshot = await uploadBytes(audioFileRef, binary);
		const url = await getDownloadURL(snapshot.ref);
		const path = snapshot.metadata.fullPath;

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

		console.log("🌱 - Still processing");
		const backendRequest = await evaluate({answer: path, question: fields.question}, task);
		console.log("🌱 - Process complete");

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

		const solutions = correspondingStat.solutions.map((x) => ({
			...x,
			evaluation: backendRequest.data,
			solution: url,
		}));

		await setDoc(
			doc(db, "stats", fields.id),
			{
				solutions,
				score: {
					correct: speakingReverseMarking[backendRequest.data.overall || 0] || 0,
					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: {answer: string; question: string}, task: number): Promise<AxiosResponse> {
	const backendRequest = await axios.post(`${process.env.BACKEND_URL}/speaking_task_2`, body, {
		headers: {
			Authorization: `Bearer ${process.env.BACKEND_JWT}`,
		},
	});

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

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