import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

/**
 * Like {@link JwtGuard}, but never rejects the request.
 *
 * When a valid `Authorization: Bearer <jwt>` is present, `request.user` is
 * populated from the JWT strategy (`{ userId, ... }`). When the token is
 * missing or invalid, the request still proceeds with `request.user`
 * undefined — the handler decides what to do.
 *
 * Used on `POST /fayda/verification/start`, which must work for both
 * logged-in users (who can opt to save the verification to their account)
 * and guests (anchored to a booking only).
 */
@Injectable()
export class OptionalJwtGuard extends AuthGuard('jwt') {
  handleRequest<TUser = unknown>(_err: unknown, user: TUser): TUser {
    return (user ?? null) as TUser;
  }
}
