import { Injectable } from "@nestjs/common";
import axios from "axios";

@Injectable()
export class SendOtpService {
  async sendOtp(mobileNumber: string, otpTemplateId: string): Promise<any> {
    const url = `https://control.msg91.com/api/v5/otp?template_id=${otpTemplateId}&mobile=${mobileNumber}&otp_length=6`;
    const headers = {
      accept: "application/json",
      "content-type": "application/json",
      authkey: "403400AF0OnTHLXvN691d649fP1",
    };
    const data = {
      Param1: "var",
    };
    try {
      const response = await axios.post(url, data, { headers });

      if (response.status !== 200) {
        return {
          status_code: response.status,
          status: "failed",
          message: "Failed to send OTP",
          response: response.data,
        };
      } else {
        return {
          status_code: response.status,
          status: "success",
          message: "OTP sent successfully",
          response: response.data,
        };
      }
    } catch (error) {
      throw error;
    }
  }
}
