const express = require('express');
const router = express.Router();
const controllers = require('../controllers');

router.get('/', (req, res, next) => {
  res.json({
    confirmation: 'sucess',
    message: 'Welcome to Lovelace Cluster API'
  });
});

router.get('/:resource', (req, res, next) => {
  const resource = req.params.resource;
  const controller = controllers[resource];

  if (controller == null) {
    res.json({
      confirmation: 'fail',
      message: 'Invalid Resource Request: ' + controller
    });
  }

  console.log(controller);

  // res.json({
  //   confirmation: 'sucess',
  //   message: 'Users'
  // });
  controller.find(req.query, (err, results) => {
    console.log('hola');
    if (err) {
      res.json({
        confirmation: 'fail',
        message: err
      });
      return
    }

    res.json({
      confirmation: 'success',
      results: results
    });
  });
});



module.exports = router;