import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import twilio from 'twilio';

@Injectable()
export class TwilioService {
  private client: ReturnType<typeof twilio>;
  private readonly logger = new Logger(TwilioService.name);

  constructor(private readonly config: ConfigService) {
    const sid = this.config.get<string>('TWILIO_ACCOUNT_SID')!;
    const token = this.config.get<string>('TWILIO_AUTH_TOKEN')!;
    this.client = twilio(sid, token);
  }
  async sendSMS(to: string, body: string) {
    try {
      const message = await this.client.messages.create({
        body,
        from: this.config.get<string>('TWILIO_PHONE_NUMBER'),
        to,
      });
      this.logger.log(`SMS enviado a ${to}: SID ${message.sid}`);
      return message;
    } catch (error) {
      this.logger.error(`Error enviando SMS a ${to}`, error);
      throw error;
    }
  }
}
