import { Controller } from '@nestjs/common';
import { CustomerlogService } from './customerlog.service';
import { MessagePattern, RpcException } from '@nestjs/microservices';

@Controller('customerlog')
export class CustomerlogController {
  constructor(private readonly customerLogService: CustomerlogService) {}

  @MessagePattern({ cmd: 'customer_log_get_all' })
  async getAll(data: { protocol: string; host: string }): Promise<any> {
    try {
      const { protocol, host } = data;
      const getAllCustomerLogs = await this.customerLogService.getAll();
      return getAllCustomerLogs;
    } catch (err) {
      console.error(err);
      throw new RpcException('Failed to retrieve customers');
    }
  }

  @MessagePattern({ cmd: 'customer_log_add' })
  async addLog(data: { body: any }): Promise<any> {
    try {
      const { body } = data;
      const add = await this.customerLogService.addLog(body);
      return add;
    } catch (err) {
      console.error(err);
      throw new RpcException('failed to add customer!');
    }
  }

  @MessagePattern({ cmd: 'get_customer_log_by_customer_id' })
  async getCustomerLogByCustomerId(data: { idCustomer: number }): Promise<any> {
    try {
      const { idCustomer } = data;
      const fetchData =
        await this.customerLogService.getCustomerLogByCustomerId(idCustomer);

      return fetchData;
    } catch (err) {
      console.error(err);
      throw new RpcException('failed to add customer!');
    }
  }
}
