import { createParamDecorator, ExecutionContext } from '@nestjs/common';

/**
 * Pulls the authenticated user out of the request, as populated by
 * the JWT strategy.
 */
export const CurrentUser = createParamDecorator(
  (data: keyof { id: bigint; username: string } | undefined, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest<{ user?: { id: bigint; username: string } }>();
    const user = request.user;
    return data ? user?.[data] : user;
  },
);
