text: ﻿const express = require('express');
  const router = express.Router();
  const Joi = require('joi');
  const validateRequest = require('_middleware/validate-request');
  const authorize = require('_middleware/authorize')
  const userService = require('./user.service');
  var { sendMail, randString } = require('../_helpers/sendEmail');
  
  // routes
  router.post('/authenticate', authenticateSchema, authenticate);
  router.post('/register', registerSchema, register);
  router.get('/', authorize(), getAll);
  router.get('/current', authorize(), getCurrent);
  router.get('/:id', authorize(), getById);
  router.put('/:id', authorize(), updateSchema, update);
  router.delete('/:id', authorize(), _delete);
  
  router.get('/verify-account/:id', verifyAccount);
  
  module.exports = router;
  
  function verifyAccount(req, res, next) {
      let key = req.params.id;
  
      userService.verifyAccount(key)
          .then(user => {
              return "Account Verified"
          })
          .catch(next);
  }
  
  function authenticateSchema(req, res, next) {
      const schema = Joi.object({
          email: Joi.string().required(),
          password: Joi.string().required()
      });
      validateRequest(req, next, schema);
  }
  
  function authenticate(req, res, next) {
      userService.authenticate(req.body)
          .then(user => res.json(user))
          .catch(next);
  }
  
  function registerSchema(req, res, next) {
      const schema = Joi.object({
          firstName: Joi.string().required(),
          lastName: Joi.string().required(),
          email: Joi.string().required(),
          password: Joi.string().min(6).required()
      });
      validateRequest(req, next, schema);
  }
  
  function register(req, res, next) {
      let verificationKey =  randString();
      req.body.verificationKey = verificationKey;
  
      userService.create(req.body)
          .then(() => {
              let mailOpt = {
                  to: `${req.body.email}`, // list of receivers
                  subject: "Email Confirmation", // Subject line
                  html: `Click <a href="http://localhost:8080/verify-account/${verificationKey}">Here</a> to verify your email. Thanks`, // html body
              };
  
              sendMail(mailOpt);
  
              res.json({ message: 'Registration successful' })
          })
          .catch(next);
  }
  
  function getAll(req, res, next) {
      userService.getAll()
          .then(users => res.json(users))
          .catch(next);
  }
  
  function getCurrent(req, res, next) {
      res.json(req.user);
  }
  
  function getById(req, res, next) {
      userService.getById(req.params.id)
          .then(user => res.json(user))
          .catch(next);
  }
  
  function updateSchema(req, res, next) {
      const schema = Joi.object({
          firstName: Joi.string().empty(''),
          lastName: Joi.string().empty(''),
          email: Joi.string().empty(''),
          password: Joi.string().min(6).empty('')
      });
      validateRequest(req, next, schema);
  }
  
  function update(req, res, next) {
      userService.update(req.params.id, req.body)
          .then(user => res.json(user))
          .catch(next);
  }
  
  function _delete(req, res, next) {
      userService.delete(req.params.id)
          .then(() => res.json({ message: 'User deleted successfully' }))
          .catch(next);
  }