import {
  Controller,
  Get,
  Inject,
  InternalServerErrorException,
  Param,
  ParseIntPipe,
} from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { firstValueFrom } from 'rxjs';

@Controller('api/customer-verification')
export class CustomerVerificationGatewayController {
  constructor(@Inject('CUSTOMER_SERVICE') private client: ClientProxy) {}

  @Get()
  async getAll() {
    try {
      return await firstValueFrom(
        this.client.send({ cmd: 'customer_verification_getAll' }, {}),
      );
    } catch (err) {
      console.log(err);
      throw new InternalServerErrorException(
        'Something went wrong when fetching verifications',
      );
    }
  }

  @Get('/get-by-customer-id/:id')
  async getLogByCustomerId(@Param('id', ParseIntPipe) id: number) {
    try {
      return await firstValueFrom(
        this.client.send(
          { cmd: 'customer_verification_get_by_customer' },
          { id },
        ),
      );
    } catch (err) {
      console.log(err);
      throw new InternalServerErrorException(
        'Something went wrong when fetching verifications',
      );
    }
  }
}
