// sms.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0

'use strict';

// const mongoose = require('mongoose');

const libphonenumber = require('libphonenumber-js');

const { SiteService } = require('../../lib/site-lib');

class SmsService extends SiteService {

  constructor (dtp) {
    super(dtp, module.exports);
  }

  async start ( ) {
    await super.start();
    this.log.info(`starting ${module.exports.name} service`);
  }

  async stop ( ) {
    this.log.info(`stopping ${module.exports.name} service`);
    await super.stop();
  }

  async send (message) {
    this.log.info('sending SMS', message);
  }

  /**
   * 
   * @param {*} phoneNumber 
   * @returns 
   */
  async checkPhoneNumber (phoneNumber) {
    const { parsePhoneNumber } = libphonenumber;

    const phoneCheck = parsePhoneNumber(phoneNumber, 'US');
    if (!phoneCheck.isValid() || !phoneCheck.number) {
      throw new Error('Invalid phone number');
    }

    return phoneCheck; // in case caller wants full data
  }
}

module.exports = {
  slug: 'sms',
  name: 'sms',
  create: (dtp) => { return new SmsService(dtp); },
};