// 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";
import formidable from "formidable-serverless";
import fs from "fs";
import FormData from 'form-data';


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);
			res.status(500).json({ ok: false });
			return;
		}

		try {
			const formData = new FormData();
			formData.append('userId', fields.userId);
			formData.append('sessionId', fields.sessionId);
			formData.append('exerciseId', fields.exerciseId);
			formData.append('question_1', fields.question);

			const audioFile = files.audio;
			if (!audioFile || !audioFile.path) {
				throw new Error('Audio file not found in request');
			}

			const buffer = fs.readFileSync(audioFile.path);
			formData.append('audio_1', buffer, 'audio_1.wav');
			fs.rmSync(audioFile.path);

			await axios.post(
				`${process.env.BACKEND_URL}/grade/speaking/2`,
				formData,
				{
					headers: {
						...formData.getHeaders(),
						Authorization: `Bearer ${process.env.BACKEND_JWT}`,
					},
				}
			);

			res.status(200).json({ ok: true });
		} catch (error) {
			console.error('Error:', error);
			if (files.audio?.path && fs.existsSync(files.audio.path)) {
				try {
					fs.rmSync(files.audio.path);
				} catch (e) {
					console.error('Failed to cleanup file:', e);
				}
			}
			res.status(500).json({ ok: false });
		}
	});
}

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