import { type FetchCreateContextFnOptions } from '@trpc/server/adapters/fetch'
import { auth, getSanitizedHeaders } from '@/lib/auth'
import { prisma } from '@innungsapp/shared'

export async function createContext({ req }: FetchCreateContextFnOptions) {
  const session = await auth.api.getSession({ headers: await getSanitizedHeaders(req.headers) })
  return {
    req,
    session,
    prisma,
  }
}

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