import { Controller } from '@nestjs/common';
import { SmsHistoryService } from './sms-history.service';
import { MessagePattern, RpcException } from '@nestjs/microservices';

@Controller()
export class SmsHistoryController {
  constructor(private readonly smshistoryservice: SmsHistoryService) {}
  @MessagePattern({ cmd: 'sms_history_get_all' })
  async getAll(): Promise<any> {
    try {
      console.log('dxsdsdf');
      const templates = await this.smshistoryservice.getAll();
      return templates;
    } catch (err) {
      console.error('Error in history get all:', err);
      throw new RpcException('Failed to retrieve sms history');
    }
  }

  @MessagePattern({ cmd: 'sms_history_get_by_id' })
  async getById(data: { id: number }): Promise<any> {
    try {
      const { id } = data;
      const templates = await this.smshistoryservice.getById(id);
      return templates;
    } catch (err) {
      console.error('Error in sms history:', err);
      throw new RpcException('Failed to retrieve sms history');
    }
  }

  @MessagePattern({ cmd: 'sms_history_add' })
  async add(data: { body: any }): Promise<any> {
    try {
      const { body } = data;
      return await this.smshistoryservice.add(body);
    } catch (err) {
      console.error('Error in sms history:', err);
      throw new RpcException('Failed to add sms history');
    }
  }

  @MessagePattern({ cmd: 'sms_history_update' })
  async update(data: { id: number; body: any }): Promise<any> {
    try {
      const { id, body } = data;
      return await this.smshistoryservice.update(id, body);
    } catch (err) {
      console.error('Error in sms history update:', err);
      throw new RpcException('Failed to update sms history');
    }
  }

  @MessagePattern({ cmd: 'sms_history_get_by_customer_id' })
  async getHistoryByCustomerId(data: { id: number }): Promise<any> {
    try {
      const { id } = data;
      return await this.smshistoryservice.getHistoryByCustomerId(id);
    } catch (err) {
      console.error('Error in sms history get by customer id:', err);
      throw new RpcException('Failed to fetch  sms history by id');
    }
  }
}
