/**
 * SMS Service
 * Handles sending SMS notifications
 * 
 * Note: This is a placeholder implementation. 
 * Replace with actual SMS gateway integration (e.g., Twilio, Nexmo, local SMS gateway)
 */

class SMSService {
  /**
   * Send SMS to a mobile number
   * @param {string} mobile - Mobile number (with country code, e.g., +8801712345678)
   * @param {string} message - Message content
   * @returns {Promise<{success: boolean, message?: string, error?: string}>}
   */
  static async sendSMS (mobile, message) {
    try {
      if (!mobile || !message) {
        return {
          success: false,
          error: 'Mobile number and message are required'
        }
      }

      // Normalize mobile number
      let normalizedMobile = String(mobile).trim().replace(/\s+/g, '')
      
      // Ensure country code format
      if (!normalizedMobile.startsWith('+88')) {
        if (normalizedMobile.startsWith('88')) {
          normalizedMobile = `+${normalizedMobile}`
        } else {
          normalizedMobile = `+88${normalizedMobile}`
        }
      }

      // Log SMS (for development/testing)
      console.log(`📱 SMS to ${normalizedMobile}: ${message}`)

      // TODO: Integrate with actual SMS gateway
      // Example implementations:
      // 
      // Option 1: Twilio
      // const twilio = require('twilio')
      // const client = twilio(accountSid, authToken)
      // await client.messages.create({
      //   body: message,
      //   to: normalizedMobile,
      //   from: twilioPhoneNumber
      // })
      //
      // Option 2: Local SMS Gateway API
      // const axios = require('axios')
      // await axios.post('http://your-sms-gateway/api/send', {
      //   mobile: normalizedMobile,
      //   message: message
      // })

      // For now, return success (actual SMS sending should be implemented)
      return {
        success: true,
        message: 'SMS sent successfully'
      }
    } catch (error) {
      console.error('❌ Error sending SMS:', error)
      return {
        success: false,
        error: error.message || 'Failed to send SMS'
      }
    }
  }

  /**
   * Send contact creation notification SMS
   * @param {string} mobile - Mobile number
   * @param {string} contactName - Name of the contact
   * @param {boolean} userExists - Whether user already exists
   * @returns {Promise<{success: boolean, message?: string, error?: string}>}
   */
  static async sendContactCreationSMS (mobile, contactName, userExists = false) {
    let message
    
    if (userExists) {
      // SMS when user already exists
      message = `Your contact has been created successfully. Your user account already exists in the system. Thank you.`
    } else {
      // SMS when contact and user are created
      message = `Your contact and user account have been created successfully. You can now login with your mobile number. Thank you.`
    }

    return await SMSService.sendSMS(mobile, message)
  }
}

module.exports = SMSService

