import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { SMSTemplates } from './sms-template.entity';
import { Repository } from 'typeorm';

@Injectable()
export class SmsTemplateService {
  constructor(
    @InjectRepository(SMSTemplates) private smsRepo: Repository<SMSTemplates>,
  ) {}
  async getAll(): Promise<any> {
    const data = await this.smsRepo.find();
    return data;
  }

  async getById(id: number): Promise<any> {
    const data = await this.smsRepo.findBy({ idSMS_Templates: id });
    return data;
  }

  async add(body: any): Promise<any> {
    const data = await this.smsRepo.create(body);
    await this.smsRepo.save(data);
    return {
      message: 'Template adding completed!',
    };
  }

  async update(id: number, body: any): Promise<any> {
    const template = await this.smsRepo.findOneBy({ idSMS_Templates: id });
    if (!template) {
      throw new Error('SMS Template not found');
    }

    const updated = this.smsRepo.merge(template, body);
    await this.smsRepo.save(updated);

    return {
      message: 'Template update completed!',
    };
  }
}
