module.exports = {
  friendlyName: 'Send SMS via Twilio',

  inputs: {
    to: {
      type: 'string',
      required: true
    },
    body: {
      type: 'string',
      required: true
    }
  },

  exits: {
    success: {
      description: 'SMS sent successfully'
    },
    error: {
      description: 'Failed to send SMS'
    }
  },

  fn: async function ({ to, body }, exits) {
    try {
        // 1. Vérification du format du numéro
      to = to.startsWith('+') ? to : `+${to}`;
      
      const twilio = require('twilio')(
        sails.config.externalApis.twilio.accountSid,
        sails.config.externalApis.twilio.authToken
      );

      const message = await twilio.messages.create({
        body: body,
        from: sails.config.externalApis.twilio.phoneNumber,
        to: to
      });

      sails.log.info('SMS sent to', to, 'with SID', message.sid);
      return exits.success();
    } catch (err) {
      sails.log.error('Twilio error:', err);
      return exits.error(err);
    }
  }
};