// 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.error('Error parsing form:', err);
		res.status(500).json({ ok: false, error: 'Failed to parse form data' });
		return;
	  }
  
	  try {
		const formData = new FormData();
  
		if (!fields.userId || !fields.sessionId || !fields.exerciseId || !fields.task) {
		  throw new Error('Missing required fields');
		}
  
		formData.append('userId', fields.userId);
		formData.append('sessionId', fields.sessionId);
		formData.append('exerciseId', fields.exerciseId);
  
		for (const fileKey of Object.keys(files)) {
		  const indexMatch = fileKey.match(/^audio_(\d+)$/);
		  if (!indexMatch) {
			console.warn(`Skipping invalid file key: ${fileKey}`);
			continue;
		  }
  
		  const index = indexMatch[1];
		  const questionKey = `question_${index}`;
		  const audioFile = files[fileKey];
  
		  if (!audioFile || !audioFile.path) {
			throw new Error(`Invalid audio file for ${fileKey}`);
		  }
  
		  if (!fields[questionKey]) {
			throw new Error(`Missing question for audio ${index}`);
		  }
  
		  try {
			const buffer = fs.readFileSync(audioFile.path);
			formData.append(`audio_${index}`, buffer, `audio_${index}.wav`);
			formData.append(questionKey, fields[questionKey]);
			fs.rmSync(audioFile.path);
		  } catch (fileError) {
			console.error(`Error processing file ${fileKey}:`, fileError);
			throw new Error(`Failed to process audio file ${index}`);
		  }
		}
  
		await axios.post(
		  `${process.env.BACKEND_URL}/grade/speaking/${fields.task}`,
		  formData,
		  {
			headers: {
			  ...formData.getHeaders(),
			  Authorization: `Bearer ${process.env.BACKEND_JWT}`,
			},
		  }
		);
  
		res.status(200).json({ ok: true });
	  } catch (error) {
		console.error('Error processing request:', error);
		res.status(500).json({
		  ok: false,
		  error: 'Internal server error'
		});
  
		Object.keys(files).forEach(fileKey => {
		  const audioFile = files[fileKey];
		  if (audioFile && audioFile.path && fs.existsSync(audioFile.path)) {
			try {
			  fs.rmSync(audioFile.path);
			} catch (cleanupError) {
			  console.error(`Failed to clean up temp file ${audioFile.path}:`, cleanupError);
			}
		  }
		});
	  }
	});
  }

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