// 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 {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);

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/${(audioFile as any).path.replace("upload_", "")}`);

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

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

		console.log("🌱 - Still processing");
		const backendRequest = await evaluate({answers: [{question: fields.question, answer: snapshot.metadata.fullPath}]});
		fs.rmSync((audioFile as any).path);
		console.log("🌱 - Process complete");

		const correspondingStat = (await getDoc(doc(db, "stats", fields.id))).data() as Stat;
		const solutions = correspondingStat.solutions.map((x) => ({
			...x,
			evaluation: backendRequest.data,
			solution: snapshot.metadata.fullPath,
		}));
		await setDoc(
			doc(db, "stats", fields.id),
			{
				solutions,
				score: {
					correct: speakingReverseMarking[backendRequest.data.overall],
					total: 100,
					missing: 0,
				},
			},
			{merge: true},
		);
		console.log("🌱 - Updated the DB");
	});
}

async function evaluate(body: {answers: object[]}): Promise<AxiosResponse> {
	const backendRequest = await axios.post(`${process.env.BACKEND_URL}/speaking_task_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,
	},
};
