const { sendEmail } = require('../../services/email');
const { passwordReset } = require('../../templates/email');

exports.sendEmailFromExternal = async (req, res, next) => {
	try {
		const { type, email, token } = req.body;

		if (process.env.EMAIL_API_KEY !== req.headers['x-api-key']) {
			return res.status(401).json({ success: false, message: 'Invalid API key' });
		}

		if (type === 'password_reset') {
			await sendEmail({
				to: email,
				subject: 'Password Reset Request',
				html: passwordReset(token, email),
			});

			return res.status(200).json({
				success: true,
				message: 'Password reset email sent successfully',
			});
		} else {
			return res.status(400).json({ success: false, message: 'Unsupported email type' });
		}
	} catch (error) {
		next(error);
	}
};
