import { Controller } from '@nestjs/common';
import { SmsTemplateService } from './sms-template.service';
import { MessagePattern, RpcException } from '@nestjs/microservices';

@Controller()
export class SmsTemplateController {
  constructor(private readonly smsTemplateDbService: SmsTemplateService) {}

  @MessagePattern({ cmd: 'sms_template_get_all' })
  async getAll(): Promise<any> {
    try {
      console.log('dxsdsdf');
      const templates = await this.smsTemplateDbService.getAll();
      return templates;
    } catch (err) {
      console.error('Error in customer_get_all:', err);
      throw new RpcException('Failed to retrieve sms templates');
    }
  }

  @MessagePattern({ cmd: 'sms_template_get_by_id' })
  async getById(data: { id: number }): Promise<any> {
    try {
      const { id } = data;
      const templates = await this.smsTemplateDbService.getById(id);
      return templates;
    } catch (err) {
      console.error('Error in customer_get_all:', err);
      throw new RpcException('Failed to retrieve sms templates');
    }
  }

  @MessagePattern({ cmd: 'sms_template_add' })
  async add(data: { body: any }): Promise<any> {
    try {
      const { body } = data;
      return await this.smsTemplateDbService.add(body);
    } catch (err) {
      console.error('Error in customer_get_all:', err);
      throw new RpcException('Failed to add sms templates');
    }
  }

  @MessagePattern({ cmd: 'sms_template_update' })
  async update(data: { id: number; body: any }): Promise<any> {
    try {
      const { id, body } = data;
      return await this.smsTemplateDbService.update(id, body);
    } catch (err) {
      console.error('Error in sms_template_update:', err);
      throw new RpcException('Failed to update sms template');
    }
  }
}
