// sms.service.ts
import { Injectable } from '@nestjs/common';
import axios from 'axios';
import { MSG91CREDS } from 'config/msg91.config';

@Injectable()
export class SmsService {
  private authKey = process.env.MSG91_AUTH_KEY; // add in .env

  async sendSms(countryCode: string, phoneNumber: string, message: string) {
    try {
      const res = await axios.post(
        'https://api.msg91.com/api/v5/flow/',
        {
          template_id: MSG91CREDS.TEMPLATE_ID, // create template in MSG91
          sender: process.env.MSG91_SENDER_ID, // like 'BPTEST'
          short_url: '1',
          recipients: [
            {
              mobiles: `${countryCode}${phoneNumber}`,
              VAR1: message,
            },
          ],
        },
        {
          headers: {
            authkey: this.authKey,
            'Content-Type': 'application/json',
          },
        },
      );
      console.log('SMS sent:', res.data);
      return true;
    } catch (error) {
      console.error('SMS sending failed:', error.message);
      return false;
    }
  }
}
