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, sendEmail} 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 ok = await sendEmail(
			"verification",
			{
				name: req.session.user.name,
				code: short.randomUUID(6),
				email: req.session.user.email,
				environment: process.env.ENVIRONMENT,
			},
			[req.session.user.email],
			"EnCoach Verification",
		);

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