import { Controller } from '@nestjs/common';
import { MessagePattern, RpcException } from '@nestjs/microservices';
import { CustomersavingsService } from './customersavings.service';

@Controller()
export class CustomersavingsController {
  constructor(private readonly savingsService: CustomersavingsService) {}

  @MessagePattern({ cmd: 'customer_savings_get_all' })
  async getAll() {
    try {
      return await this.savingsService.getAll();
    } catch (err) {
      console.error(err);
      throw new RpcException('Failed to retrieve savings');
    }
  }

  @MessagePattern({ cmd: 'customer_savings_get_by_id' })
  async getById(data: { id: number }) {
    try {
      return await this.savingsService.getById(data.id);
    } catch (err) {
      console.error(err);
      throw new RpcException('Failed to get savings by ID');
    }
  }

  @MessagePattern({ cmd: 'customer_savings_add' })
  async add(data: any) {
    try {
      return await this.savingsService.add(data);
    } catch (err) {
      console.error(err);
      throw new RpcException('Failed to add savings');
    }
  }

  @MessagePattern({ cmd: 'customer_savings_update' })
  async update(data: any) {
    try {
      return await this.savingsService.update(data.id, data);
    } catch (err) {
      console.error(err);
      throw new RpcException('Failed to update savings');
    }
  }

  @MessagePattern({ cmd: 'customer_savings_get_by_customer' })
  async getByCustomerId(data: { customerId: number }) {
    try {
      return await this.savingsService.getByCustomerId(data.customerId);
    } catch (err) {
      console.error(err);
      throw new RpcException('Failed to get savings by customer ID');
    }
  }
}
