import { Controller } from '@nestjs/common';
import { MessagePattern, RpcException } from '@nestjs/microservices';
import { PaymentmethodsService } from './paymentmethods.service';

@Controller()
export class PaymentmethodsController {
  constructor(private readonly paymentMethodService: PaymentmethodsService) {}

  @MessagePattern({ cmd: 'payment_method_get_all' })
  async getAll() {
    try {
      return await this.paymentMethodService.getAll();
    } catch (err) {
      console.log(err);
      throw new RpcException('Failed to retrieve payment methods');
    }
  }

  @MessagePattern({ cmd: 'payment_method_get_by_id' })
  async getById(data: { id: number }) {
    try {
      return await this.paymentMethodService.getById(data.id);
    } catch (err) {
      console.log(err);
      throw new RpcException('Failed to get payment method by ID');
    }
  }

  @MessagePattern({ cmd: 'payment_method_get_by_customer' })
  async getByCustomerId(data: { id: number }) {
    try {
      return await this.paymentMethodService.getByCustomerId(data.id);
    } catch (err) {
      console.log(err);
      throw new RpcException('Failed to retrieve by customer');
    }
  }

  @MessagePattern({ cmd: 'payment_method_add' })
  async add(data: any) {
    try {
      return await this.paymentMethodService.add(data);
    } catch (err) {
      console.log(err);
      throw new RpcException('Failed to add payment method');
    }
  }

  @MessagePattern({ cmd: 'payment_method_update' })
  async update(data: any) {
    try {
      console.log('Normal controller is callingg');
      return await this.paymentMethodService.update(data.id, data);
    } catch (err) {
      console.log(err);
      throw new RpcException('Failed to update payment method');
    }
  }
}
