import {
  BadGatewayException,
  Body,
  Controller,
  Get,
  Inject,
  Param,
  ParseIntPipe,
  Post,
  Put,
} from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { catchError, throwError } from 'rxjs';

@Controller('api/location')
export class MerchantlocationController {
  constructor(@Inject('MERCHANT_LOCATION') private client: ClientProxy) {}

  @Get(':id')
  async getById(@Param('id', ParseIntPipe) id: number) {
    console.log('ok!@');
    return this.client
      .send({ cmd: 'merchant_location_get_by_id' }, { id })
      .pipe(
        catchError((error) => throwError(() => new BadGatewayException(error))),
      );
  }

  @Get()
  async getAll() {
    return this.client
      .send({ cmd: 'merchant_location_get_all' }, {})
      .pipe(
        catchError((error) => throwError(() => new BadGatewayException(error))),
      );
  }

  @Post()
  async createMerchantLocation(@Body() body: any) {
    return this.client.send({ cmd: 'merchant_location_add' }, { body });
  }

  @Put(':id')
  async updateLocation(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: any,
  ) {
    return this.client.send({ cmd: 'merchant_location_update' }, { id, body });
  }
}
