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

export interface JwtPayload {
  sub: string;
  clubId: string;
  role: string;
  roles: string[];
}

/**
 * Extract the current user from the request (populated by JwtAuthGuard).
 *
 * @example
 * async getProfile(@CurrentUser() user: JwtPayload) {
 *   return this.usersService.findById(user.sub);
 * }
 *
 * @example
 * async getClub(@CurrentUser('clubId') clubId: string) { ... }
 */
export const CurrentUser = createParamDecorator(
  (data: keyof JwtPayload | undefined, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest<{ user: JwtPayload }>();
    const user = request.user;
    return data ? user?.[data] : user;
  },
);
