//const Informations = require("../../models/Informations");

module.exports = {

  friendlyName: 'Create',

  description: 'Create user.',

  inputs: {
    nom: {
      type: 'string',
      required: true
    },
    prenom: {
      type: 'string',
      required: true
    },
    sexe: {
      type: 'string',
      isIn: ['M', 'F']
    },
    email: {
      type: 'string',
      required: true

    },

    dateNaissance: {
      type: 'string'
    },
    qualification: {
      type: 'string',
      //required: true
    },

    adresse: {
      type: 'string',
      //required: true
    },

    tel1: {
      type: 'string',
      
    },
    tel2: {
      type: 'string',
      //required: true
    },
    estSuperviseur: {
      type: 'boolean',
      //required: true
    },
    /*role: {
      type: 'number',
      //required: true
    },*/
    roleSlug: {
      type: 'string',
      required: true
    },
    section: {
      type: 'number',
      //required: true
    },

  },

  exits: {
    fail: {
      statusCode: 400
    },
    notFound: {
      statusCode: 404
    },
    encryptionError: {
      statusCode: 500,
      message: "Erreur encryptage"
    },
    conflict: {
      statusCode: 409
    }
  },

  fn: async function (inputs, exits) {
    // Définir constante

    let usernameVerify = await User.find({ username: inputs.email }).then(_.first)
    if (usernameVerify) {
      return exits.notFound({ message: "Ce Mail existe déjà, Veuillez changer!" })
    }
    
    let userRole = await Role.find({ slug: inputs.roleSlug }).then(_.first)
    if (!userRole) {
      throw { notFound: { message: 'Role non retrouvé!' } }
    }

    let generatedPasss = generate()
    let password// = generatedPasss;
    await sails.config.cryptoUtils.encrypt(generatedPasss,function (err, msg){
      if(err){
        throw 'encryptionError';
      }
      //console.log('encrypted pass', msg) 
      password = msg
    })


    let user = await User.create({
      //id: idUser,
      nom: inputs.nom.toUpperCase(),
      prenom: inputs.prenom,
      email: inputs.email,
      dateNaissance: inputs.dateNaissance,
      qualification: inputs.qualification,
      username: inputs.email,
      password: password,
      sexe: inputs.sexe,
      tel1: inputs.tel1,
      tel2: inputs.tel2,
      estSuperviseur: inputs.estSuperviseur,
      role: userRole.id,
      roleSlug: inputs.roleSlug,
      section: inputs.section,
      nomPrenom: `${inputs.nom.toUpperCase()} ${inputs.prenom.toUpperCase()}`
      
    }).fetch()
    if(!user){
      throw {fail:{message:'Echec enregistrement des infos utilisateur'}}
    } 

    if(inputs.email){
      await sails.helpers.sendSmtp.with({
        subject:"Identifiants de connexion/E-Gestion/Evolutio System",
        to: inputs.email,
        text:`Votre identifiant de connexion est  : ${inputs.email} <br>
         Mot de passe : ${generatedPasss}
        `
      })
    }


    // All done.
    return exits.success(user);
  }
}

function generate(number = 4) {
  let text = '';
  let possible = "1254785421458965231254789563254178542130012447820177";
  for (let i = 0; i < number; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}
