import { initTRPC, TRPCError } from '@trpc/server';
import superjson from 'superjson';
import { getCurrentUser } from '@crafted/auth';

interface Context {
  userId?: string;
  user?: {
    id: string;
    email: string;
    name?: string;
  };
}

const t = initTRPC.context<Context>().create({
  transformer: superjson,
});

const isAuthed = t.middleware(async ({ ctx, next }) => {
  const user = await getCurrentUser();

  if (!user) {
    throw new TRPCError({ code: 'UNAUTHORIZED' });
  }

  return next({
    ctx: {
      userId: user.id,
      user,
    },
  });
});

export const router = t.router;
export const publicProcedure = t.procedure;
export const protectedProcedure = t.procedure.use(isAuthed);
