import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';

export const POST: RequestHandler = async ({ locals, url }) => {
	const { session, user } = await locals.safeGetSession();

	if (!session || !user) {
		return json({ error: 'Not authenticated' }, { status: 401 });
	}

	if (!user.email) {
		return json({ error: 'No email associated with account' }, { status: 400 });
	}

	// Resend verification email
	const { error } = await locals.supabase.auth.resend({
		type: 'signup',
		email: user.email,
		options: {
			emailRedirectTo: `${url.origin}/join?verified=true`
		}
	});

	if (error) {
		console.error('Failed to resend verification email:', error);
		return json({ error: error.message }, { status: 500 });
	}

	return json({ success: true, message: 'Verification email sent' });
};
