import { auth } from "@kk/auth";
import { env } from "@kk/env/server";
import type { EmailMessage } from "./email-queue";

// CF Workers runtime Queue type (not the alchemy resource type)
type Queue = {
	send(
		message: EmailMessage,
		options?: { contentType?: string },
	): Promise<void>;
	sendBatch(
		messages: Array<{ body: EmailMessage }>,
		options?: { contentType?: string },
	): Promise<void>;
};

export async function createContext({
	req,
	emailQueue,
}: {
	req: Request;
	emailQueue?: Queue;
}) {
	const session = await auth.api.getSession({
		headers: req.headers,
	});
	return {
		session,
		env,
		emailQueue,
	};
}

export type Context = Awaited<ReturnType<typeof createContext>>;
