import {
  Body,
  Controller,
  Get,
  Inject,
  InternalServerErrorException,
  Param,
  ParseIntPipe,
  Post,
  Req,
} from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { Request } from 'express';
import { firstValueFrom } from 'rxjs';
@Controller('api/merchant-log')
export class MerchantLogGatewayController {
  constructor(@Inject('MERCHANT_LOG') private client: ClientProxy) {}

  @Get()
  async getAllMerchantLogs(@Req() req: Request) {
    try {
      const protocol = req.protocol;
      const host = req.headers.host;

      return await firstValueFrom(
        this.client.send({ cmd: 'merchant_log_get_all' }, { protocol, host }),
      );
    } catch (err) {
      console.log(err);
      throw new InternalServerErrorException('Something went wrong!');
    }
  }

  @Post()
  async addCustomerLogs(@Body() body: any) {
    try {
      console.log('here calling as expected!');
      const result = await firstValueFrom(
        this.client.send({ cmd: 'merchant_log_add' }, { body }),
      );
      return result;
    } catch (err) {
      console.log(err);
      throw new InternalServerErrorException(err);
    }
  }

  @Get('/get-logs-by-merchant-id/:idMerchant')
  async getLogsByMerchantId(
    @Param('idMerchant', ParseIntPipe) idMerchant: number,
  ) {
    try {
      console.log('hmm');
      const result = await firstValueFrom(
        this.client.send(
          { cmd: 'get_merchant_log_by_merchant_id' },
          { idMerchant },
        ),
      );
      return result;
    } catch (err) {
      console.log(err);
      throw new InternalServerErrorException(err);
    }
  }
}
