import { Injectable, Inject, UnauthorizedException } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { firstValueFrom } from 'rxjs';

@Injectable()
export class AuthValidationService {
  constructor(@Inject('AUTH_SERVICE') private readonly client: ClientProxy) {}

  async validateUserCredintials(Email: string, Password: string) {
    try {
      console.log(Email);
      console.log(Password);
      const response = await firstValueFrom(
        this.client.send(
          { cmd: 'validate_user_credentials' },
          { Email, Password },
        ),
      );
      return response?.user || null;
    } catch (err) {
      const message = err || 'Invalid credentials';
      throw new UnauthorizedException(message); // Makes Nest send 401 with your message
    }
  }
}
