import { auth } from "$lib/domains/auth/config/base";
import { UserController } from "$lib/domains/user/domain/controller";
import type { RequestEvent } from "@sveltejs/kit";
import type { inferAsyncReturnType } from "@trpc/server";

export async function createContext(event: RequestEvent) {
	const result = await auth.api.getSession({
		headers: event.request.headers,
	});

	if (!result || !result.user || !result.session) {
		return {};
	}

	const uc = new UserController();
	const uOut = await uc.getUserById(result.user.id);
	if (uOut.error) {
		return {};
	}

	return { session: result.session, user: uOut.data };
}

export type Context = inferAsyncReturnType<typeof createContext>;
