import { redirect } from '@sveltejs/kit';
import { form, getRequestEvent, query } from '$app/server';
import { auth } from '$lib/server/auth';
import { signupSchema, loginSchema } from '$lib/schema/auth';
import { APIError } from 'better-auth';

export const signup = form(signupSchema, async (user) => {
	await auth.api.signUpEmail({ body: user });
	redirect(307, '/verify-email');
});

export const login = form(loginSchema, async (user) => {
	const { request, url } = getRequestEvent();
	try {
		await auth.api.signInEmail({ body: user, headers: request.headers });
	} catch (err: unknown) {
		if (
			err instanceof APIError &&
			err.body?.message?.toLowerCase().includes('email not verified')
		) {
			redirect(307, '/verify-email');
		}
		throw err;
	}
	const next = url.searchParams.get('redirect') || '/dashboard';
	const safeNext = next.startsWith('/') && !next.startsWith('//') ? next : '/dashboard';
	redirect(303, safeNext);
});

export const signout = form(async () => {
	const { request } = getRequestEvent();
	await auth.api.signOut({ headers: request.headers });
	redirect(303, '/login');
});

export const getUser = query(async () => {
	const { locals } = getRequestEvent();
	if (!locals.user) {
		redirect(307, '/login');
	}
	return locals.user;
});
