import {NextApiRequest, NextApiResponse} from "next";
import {getAuth, sendEmailVerification, sendSignInLinkToEmail, User} from "firebase/auth";
import {getAuth as getAdminAuth, UserRecord} from "firebase-admin/auth";
import {app, adminApp} from "@/firebase";
import {sessionOptions} from "@/lib/session";
import {withIronSessionApiRoute} from "iron-session/next";
import {prepareMailer, prepareMailOptions} from "@/email";
import ShortUniqueId from "short-unique-id";

export default withIronSessionApiRoute(sendVerification, sessionOptions);

async function sendVerification(req: NextApiRequest, res: NextApiResponse) {
	const short = new ShortUniqueId();

	if (req.session.user) {
		const transport = prepareMailer("verification");
		const mailOptions = prepareMailOptions(
			{
				name: req.session.user.name,
				code: short.randomUUID(6),
				email: req.session.user.email,
			},
			[req.session.user.email],
			"EnCoach Verification",
			"verification",
		);

		await transport.sendMail(mailOptions);

		res.status(200).json({ok: true});
		return;
	}
	res.status(404).json({ok: false});
}
