const axios = require('axios')

// Helper function to format SMS date
function formatSMSDate(date) {
  return new Date(date).toLocaleString('fa-IR', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

class SmsService {
  constructor(apiKey) {
    this.API_URL_FAST = 'https://api.sms.ir/v1'
    this.OTP_PATTERN = '83604'
    this.TRANSACTION_SUBMITTED_PATTERN = '565621'
    this.TRANSACTION_RECEIVED_PATTERN = '700980'
    this.TRANSACTION_ONGOING_PATTERN = '359158'
    this.TRANSACTION_WAITING_PATTERN = '595406'
    this.TRANSACTION_DELIVERED_PATTERN = '921981'
    //
    this.DEVICE_LOGIN_PATTERN = '970395' // ورود با دستگاه جدید
    this.OTP_VERIFICATION_PATTERN = '403891' // تأیید شماره موبایل
    this.MOBILE_VERIFICATION_CODE_PATTERN = '795491' // کد تایید شماره همراه
    this.PASSWORD_RESET_PATTERN = '252168' // بازیابی کلمه عبور
    //
    this.LOTTERY_ENROLLMENT_PATTERN = '254556' // ثبت نام در قرعه کشی
    this.WARRANTY_RECEPTION_PATTERN = '239482' // دریافت کالا برای گارانتی
    this.WARRANTY_REPAIR_COMPLETED_PATTERN = '968726' // اتمام تعمیر گارانتی
    this.WARRANTY_SENDBACK_PATTERN = '128079' // ارسال کالا پس از گارانتی
    //
    this.PIECE_REQUEST_SENT_PATTERN = '656765' // ارسال درخواست قطعه
    this.PIECE_REQUEST_ONGOING_PATTERN = '153240' // در دست اقدام درخواست قطعه
    this.PIECE_REQUEST_WAITING_PATTERN = '611156' // انتظار درخواست قطعه
    this.PIECE_REQUEST_DELIVERED_PATTERN = '312081' // تحویل درخواست قطعه
    //
    this.REPRESENTATION_SUBMITTED_PATTERN = '117321' // ثبت درخواست نمایندگی
    this.REPRESENTATION_STATUS_UPDATE_PATTERN = '765284' // به‌روزرسانی وضعیت نمایندگی
    this.REPRESENTATION_UPLOAD_ACCESS_PATTERN = '564752' // دسترسی آپلود نمایندگی
    this.REPRESENTATION_EDIT_ACCESS_PATTERN = '863757' // دسترسی ویرایش نمایندگی
    this.REPRESENTATION_UNDER_REVIEW_PATTERN = '800883' // نمایندگی در حال بررسی
    //
    // Guarantee Report Patterns
    this.GUARANTEE_REPORT_SENT_PATTERN = '752621' // ارسال گزارش گارانتی
    this.GUARANTEE_FORM_DELIVERED_PATTERN = '752315' // تحویل فرم گارانتی
    this.GUARANTEE_FORM_WAITING_PATTERN = '903583' // در انتظار قطعه برای فرم گارانتی
    this.GUARANTEE_FORM_ONGOING_PATTERN = '495824' // اقدام برای فرم گارانتی
    this.GUARANTEE_FORM_OFFICE_DELIVERY_PATTERN = '518885' // تحویل فرم گارانتی به دفتر
    this.GUARANTEE_FORM_REGISTRATION_PATTERN = '146994' // ثبت فرم گارانتی

    //
    this.OLD_PIECE_REQUEST_PATTERN = '859135' // ارسال درخواست قطعه داغی
    this.OLD_PIECE_REQUEST_ONGOING_PATTERN = '474942' // در دست اقدام بودن قطعه داغی
    this.OLD_PIECE_REQUEST_WAITING_PATTERN = '575103' // انتظار قطعه برای قطعه داغی
    this.OLD_PIECE_REQUEST_DELIVERED_PATTERN = '262772' // تحویل قطعه داغی
    // this.OLD_PIECE_REQUEST_CANCELED_PATTERN = '83619'
    this.BROADCAST_PATTERN = '83615'
    this.BUFFER_REQUEST_PATTERN = '981226' // ارسال درخواست کالای بافر
    this.BUFFER_REQUEST_ONGOING_PATTERN = '438301' // در دست اقدام بودن کالای بافر
    this.BUFFER_REQUEST_WAITING_PATTERN = '300864' // انتظار قطعه برای کالای بافر
    this.BUFFER_REQUEST_DELIVERED_PATTERN = '649466' // تحویل کالای بافر
    this.SMS_API_KEY = apiKey
  }

  async sendOTPSms(otp, phone) {
    const smsData = {
      Parameters: [{ name: 'code', value: `${otp}` }],
      Mobile: phone,
      TemplateId: this.OTP_PATTERN
    }
    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending OTP SMS', error)
      throw error
    }
  }

  /// ////////////////////////////////////////////////////////////
  async sendTransactionSubmittedSMS(mobile, transactionNumber, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'transactionNumber', value: `${transactionNumber}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.TRANSACTION_SUBMITTED_PATTERN
    }
    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Transaction SMS', error)
      throw error
    }
  }
  /// ////////////////////////////////////////////////////////////

  async sendTransactionReceivedSMS(mobile, tempNumber, receiptNumber, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'tempNumber', value: `${tempNumber}` },
        { name: 'receiptNumber', value: `${receiptNumber}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.TRANSACTION_RECEIVED_PATTERN
    }
    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Transaction Received SMS', error.response.data)
      throw error
    }
  }

  async sendTransactionOngoingSMS(mobile, receiptNumber, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'receiptNumber', value: `${receiptNumber}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.TRANSACTION_ONGOING_PATTERN
    }
    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Transaction Ongoing SMS', error)
      throw error
    }
  }

  async sendTransactionWaitingSMS(mobile, receiptNumber, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'receiptNumber', value: `${receiptNumber}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.TRANSACTION_WAITING_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Transaction Waiting SMS', error)
      throw error
    }
  }
  /// ////////////////////////////////////////////////////////////

  async sendTransactionDeliveredSMS(mobile, receiptNumber, deliveryCode = '', date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'receiptNumber', value: `${receiptNumber}` },
        { name: 'deliveryCode', value: `${deliveryCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.TRANSACTION_DELIVERED_PATTERN
    }
    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Transaction Delivered SMS', error)
      throw error
    }
  }

  async sendDeviceLoginSMS(mobile, deviceName) {
    const smsData = {
      Parameters: [{ name: 'deviceName', value: `${deviceName}` }],
      Mobile: mobile,
      TemplateId: this.DEVICE_LOGIN_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Device Login SMS', error)
      throw error
    }
  }

  async sendOTPVerificationSMS(mobile, otp, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'otp', value: `${otp}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.OTP_VERIFICATION_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending OTP Verification SMS', error)
      throw error
    }
  }

  async sendGuaranteeReportSentSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.GUARANTEE_REPORT_SENT_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Guarantee Report SMS', error)
      throw error
    }
  }

  async sendGuaranteeFormDeliveredSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.GUARANTEE_FORM_DELIVERED_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Guarantee Form Delivered SMS', error)
      throw error
    }
  }

  async sendGuaranteeFormWaitingSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.GUARANTEE_FORM_WAITING_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Guarantee Form Waiting SMS', error)
      throw error
    }
  }

  async sendGuaranteeFormOngoingSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.GUARANTEE_FORM_ONGOING_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Guarantee Form Ongoing SMS', error)
      throw error
    }
  }

  async sendGuaranteeFormOfficeDeliverySMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.GUARANTEE_FORM_OFFICE_DELIVERY_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Guarantee Form Office Delivery SMS', error)
      throw error
    }
  }

  async sendGuaranteeFormRegistrationSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.GUARANTEE_FORM_REGISTRATION_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Guarantee Form Registration SMS', error)
      throw error
    }
  }

  async sendMobileVerificationCodeSMS(mobile, mobileKey, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'MOBILEKEY', value: `${mobileKey}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.MOBILE_VERIFICATION_CODE_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Mobile Verification Code SMS', error)
      throw error
    }
  }

  async sendPasswordResetSMS(mobile, resetLink, date = new Date()) {
    console.log(date)
    const smsData = {
      Parameters: [{ name: 'resetlink', value: `${resetLink}` }],
      Mobile: mobile,
      TemplateId: this.PASSWORD_RESET_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      console.log(data)
      return data
    } catch (error) {
      console.error('Error in sending Password Reset SMS', error)
      console.log(error)
      throw error
    }
  }

  async sendLotteryEnrollmentSMS(mobile, fullName, enrollCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'fullName', value: `${fullName}` },
        { name: 'enrollCode', value: `${enrollCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.LOTTERY_ENROLLMENT_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Lottery Enrollment SMS', error)
      throw error
    }
  }

  async sendWarrantyReceptionSMS(mobile, fullName, tempReceiptNumber, permanentReceiptNumber, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'fullName', value: `${fullName}` },
        { name: 'tempReceiptNumber', value: `${tempReceiptNumber}` },
        { name: 'permanentReceiptNumber', value: `${permanentReceiptNumber}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.WARRANTY_RECEPTION_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Warranty Reception SMS', error)
      throw error
    }
  }

  async sendWarrantyRepairCompletedSMS(mobile, fullName, itemReceptionNumber, invoiceNumber, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'fullName', value: `${fullName}` },
        { name: 'itemReceptionNumber', value: `${itemReceptionNumber}` },
        { name: 'invoiceNumber', value: `${invoiceNumber}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.WARRANTY_REPAIR_COMPLETED_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Warranty Repair Completed SMS', error)
      throw error
    }
  }

  async sendWarrantySendbackSMS(mobile, fullName, itemReceptionNumber, deliveryCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'fullName', value: `${fullName}` },
        { name: 'itemReceptionNumber', value: `${itemReceptionNumber}` },
        { name: 'deliveryCode', value: `${deliveryCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.WARRANTY_SENDBACK_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Warranty Sendback SMS', error)
      throw error
    }
  }

  async sendPieceRequestSentSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.PIECE_REQUEST_SENT_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Piece Request Sent SMS', error)
      throw error
    }
  }

  async sendPieceRequestOngoingSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.PIECE_REQUEST_ONGOING_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Piece Request Ongoing SMS', error)
      throw error
    }
  }

  async sendPieceRequestWaitingSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.PIECE_REQUEST_WAITING_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Piece Request Waiting SMS', error)
      throw error
    }
  }

  async sendPieceRequestDeliveredSMS(mobile, trackingCode, deliveryCode = '', date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'deliveryCode', value: `${deliveryCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.PIECE_REQUEST_DELIVERED_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Piece Request Delivered SMS', error)
      throw error
    }
  }

  async sendRepresentationSubmittedSMS(mobile, representationCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'representationCode', value: `${representationCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.REPRESENTATION_SUBMITTED_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Representation Submitted SMS', error)
      throw error
    }
  }

  async sendRepresentationStatusUpdateSMS(mobile, representationCode, statusPhrase, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'representationCode', value: `${representationCode}` },
        { name: 'statusPhrase', value: `${statusPhrase}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.REPRESENTATION_STATUS_UPDATE_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Representation Status Update SMS', error)
      throw error
    }
  }

  async sendRepresentationUploadAccessSMS(mobile, representationCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'representationCode', value: `${representationCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.REPRESENTATION_UPLOAD_ACCESS_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Representation Upload Access SMS', error)
      throw error
    }
  }

  async sendRepresentationEditAccessSMS(mobile, date = new Date()) {
    const smsData = {
      Parameters: [{ name: 'date', value: `${formatSMSDate(date)}` }],
      Mobile: mobile,
      TemplateId: this.REPRESENTATION_EDIT_ACCESS_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Representation Edit Access SMS', error)
      throw error
    }
  }

  async sendRepresentationUnderReviewSMS(mobile, representationCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'representationCode', value: `${representationCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.REPRESENTATION_UNDER_REVIEW_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Representation Under Review SMS', error)
      throw error
    }
  }

  async sendOldPieceRequestSentSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.OLD_PIECE_REQUEST_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Old Piece Request SMS', error)
      throw error
    }
  }

  async sendOldPieceRequestOngoingSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.OLD_PIECE_REQUEST_ONGOING_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Old Piece Request Ongoing SMS', error)
      throw error
    }
  }

  async sendOldPieceRequestWaitingSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.OLD_PIECE_REQUEST_WAITING_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Old Piece Request Waiting SMS', error)
      throw error
    }
  }

  async sendOldPieceRequestDeliveredSMS(mobile, trackingCode, deliveryCode = '', date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` },
        { name: 'deliveryCode', value: `${deliveryCode}` }
      ],
      Mobile: mobile,
      TemplateId: this.OLD_PIECE_REQUEST_DELIVERED_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Old Piece Request Delivered SMS', error)
      throw error
    }
  }

  /// ////////////////////////////////////////////////////////////

  async sendBufferRequestSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.BUFFER_REQUEST_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Buffer Request SMS', error)
      throw error
    }
  }

  async sendBufferRequestOngoingSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.BUFFER_REQUEST_ONGOING_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Buffer Request Ongoing SMS', error)
      throw error
    }
  }

  async sendBufferRequestWaitingSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.BUFFER_REQUEST_WAITING_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Buffer Request Waiting SMS', error)
      throw error
    }
  }

  async sendBufferRequestDeliveredSMS(mobile, trackingCode, deliveryCode = '', date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` },
        { name: 'deliveryCode', value: `${deliveryCode}` }
      ],
      Mobile: mobile,
      TemplateId: this.BUFFER_REQUEST_DELIVERED_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Buffer Request Delivered SMS', error)
      throw error
    }
  }

  async sendBroadcastSMS(mobile, message) {
    const smsData = {
      Parameters: [{ name: 'message', value: `${message}` }],
      Mobile: mobile,
      TemplateId: this.BROADCAST_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Broadcast SMS', error)
      throw error
    }
  }

  async sendOldPieceRequestCanceledSMS(mobile, trackingCode, date = new Date()) {
    const smsData = {
      Parameters: [
        { name: 'trackingCode', value: `${trackingCode}` },
        { name: 'date', value: `${formatSMSDate(date)}` }
      ],
      Mobile: mobile,
      TemplateId: this.OLD_PIECE_REQUEST_CANCELED_PATTERN
    }

    try {
      const { data } = await axios.post(`${this.API_URL_FAST}/send/verify`, smsData, {
        headers: {
          'X-API-KEY': this.SMS_API_KEY,
          'Content-Type': 'application/json'
        }
      })
      return data
    } catch (error) {
      console.error('Error in sending Old Piece Request Canceled SMS', error)
      throw error
    }
  }

  /// ////////////////////////////////////////////////////////////
}

// Create a shared SMS service instance
const smsService = new SmsService('QkkNxhyXZ6GtEf1soOTtikomO3mA4LaNQDH8mol8huDIwh00')

// Clean SMS service exports - organized structure
module.exports = {
  // Service class export for advanced usage
  SmsService,

  // Organized SMS methods by category
  auth: {
    sendOTP: (mobile, otp) => smsService.sendOTPSms(otp, mobile),
    deviceLogin: (mobile, deviceName) => smsService.sendDeviceLoginSMS(mobile, deviceName),
    otpVerification: (mobile, otp, date) => smsService.sendOTPVerificationSMS(mobile, otp, date),
    mobileVerificationCode: (mobile, mobileKey, date) =>
      smsService.sendMobileVerificationCodeSMS(mobile, mobileKey, date),
    passwordReset: (mobile, resetUrl, date) => smsService.sendPasswordResetSMS(mobile, resetUrl, date)
  },

  transaction: {
    submitted: (mobile, transactionNumber, date) =>
      smsService.sendTransactionSubmittedSMS(mobile, transactionNumber, date),
    received: (mobile, tempNumber, receiptNumber, date) =>
      smsService.sendTransactionReceivedSMS(mobile, tempNumber, receiptNumber, date),
    ongoing: (mobile, receiptNumber, date) => smsService.sendTransactionOngoingSMS(mobile, receiptNumber, date),
    waiting: (mobile, receiptNumber, date) => smsService.sendTransactionWaitingSMS(mobile, receiptNumber, date),
    delivered: (mobile, receiptNumber, deliveryCode, date) =>
      smsService.sendTransactionDeliveredSMS(mobile, receiptNumber, deliveryCode, date)
  },

  bufferRequest: {
    sent: (mobile, trackingCode, date) => smsService.sendBufferRequestSMS(mobile, trackingCode, date),
    ongoing: (mobile, trackingCode, date) => smsService.sendBufferRequestOngoingSMS(mobile, trackingCode, date),
    waiting: (mobile, trackingCode, date) => smsService.sendBufferRequestWaitingSMS(mobile, trackingCode, date),
    delivered: (mobile, trackingCode, deliveryCode, date) =>
      smsService.sendBufferRequestDeliveredSMS(mobile, trackingCode, deliveryCode, date)
  },

  oldPieceRequest: {
    sent: (mobile, trackingCode, date) => smsService.sendOldPieceRequestSentSMS(mobile, trackingCode, date),
    ongoing: (mobile, trackingCode, date) => smsService.sendOldPieceRequestOngoingSMS(mobile, trackingCode, date),
    waiting: (mobile, trackingCode, date) => smsService.sendOldPieceRequestWaitingSMS(mobile, trackingCode, date),
    delivered: (mobile, trackingCode, deliveryCode, date) =>
      smsService.sendOldPieceRequestDeliveredSMS(mobile, trackingCode, deliveryCode, date),
    canceled: (mobile, trackingCode, date) => smsService.sendOldPieceRequestCanceledSMS(mobile, trackingCode, date)
  },

  guaranteeReport: {
    sent: (mobile, trackingCode, date) => smsService.sendGuaranteeReportSentSMS(mobile, trackingCode, date)
  },

  guaranteeForm: {
    delivered: (mobile, trackingCode, date) => smsService.sendGuaranteeFormDeliveredSMS(mobile, trackingCode, date),
    waiting: (mobile, trackingCode, date) => smsService.sendGuaranteeFormWaitingSMS(mobile, trackingCode, date),
    ongoing: (mobile, trackingCode, date) => smsService.sendGuaranteeFormOngoingSMS(mobile, trackingCode, date),
    officeDelivery: (mobile, trackingCode, date) =>
      smsService.sendGuaranteeFormOfficeDeliverySMS(mobile, trackingCode, date),
    registration: (mobile, trackingCode, date) =>
      smsService.sendGuaranteeFormRegistrationSMS(mobile, trackingCode, date)
  },
  /// ///////////////////////////////////////////////////////////
  lottery: {
    enrollment: (mobile, fullName, enrollCode, date) =>
      smsService.sendLotteryEnrollmentSMS(mobile, fullName, enrollCode, date)
  },

  warranty: {
    reception: (mobile, fullName, tempReceiptNumber, permanentReceiptNumber, date) =>
      smsService.sendWarrantyReceptionSMS(mobile, fullName, tempReceiptNumber, permanentReceiptNumber, date),
    repairCompleted: (mobile, fullName, itemReceptionNumber, invoiceNumber, date) =>
      smsService.sendWarrantyRepairCompletedSMS(mobile, fullName, itemReceptionNumber, invoiceNumber, date),
    sendback: (mobile, fullName, itemReceptionNumber, deliveryCode, date) =>
      smsService.sendWarrantySendbackSMS(mobile, fullName, itemReceptionNumber, deliveryCode, date)
  },

  pieceRequest: {
    sent: (mobile, trackingCode, date) => smsService.sendPieceRequestSentSMS(mobile, trackingCode, date),
    ongoing: (mobile, trackingCode, date) => smsService.sendPieceRequestOngoingSMS(mobile, trackingCode, date),
    waiting: (mobile, trackingCode, date) => smsService.sendPieceRequestWaitingSMS(mobile, trackingCode, date),
    delivered: (mobile, trackingCode, deliveryCode, date) =>
      smsService.sendPieceRequestDeliveredSMS(mobile, trackingCode, deliveryCode, date)
  },

  representation: {
    submitted: (mobile, representationCode, date) =>
      smsService.sendRepresentationSubmittedSMS(mobile, representationCode, date),
    statusUpdate: (mobile, representationCode, statusPhrase, date) =>
      smsService.sendRepresentationStatusUpdateSMS(mobile, representationCode, statusPhrase, date),
    uploadAccess: (mobile, representationCode, date) =>
      smsService.sendRepresentationUploadAccessSMS(mobile, representationCode, date),
    editAccess: (mobile, date) => smsService.sendRepresentationEditAccessSMS(mobile, date),
    underReview: (mobile, representationCode, date) =>
      smsService.sendRepresentationUnderReviewSMS(mobile, representationCode, date)
  },

  broadcast: (mobile, message) => smsService.sendBroadcastSMS(mobile, message)
}
