const express = require('express'),
  router = express.Router();

const originalGroups = require('../group-data.js');
const originalEvents = require('../event-data.js');

router.get('/:group?', async (req, res/*, next*/) => {
  const { group } = req.params;
  if (!group) {
    return res.status(200).send(originalGroups);
  }
  const found = originalGroups.find((item) => {
    if (typeof group === 'number') {
      return item.id === group;
    }
    if (typeof group === 'string') {
      return item.group === group;
    }
    return false;
  });
  if (!found) {
    return res.status(404).send({ message: `Unable to find ${group}.`});
  }
  return res.status(200).send([ found ]);
});

router.get('/:group/events', async (req, res/*, next*/) => {
  return res.status(200).send(originalEvents);
});

router.post('/:group', async (req, res) => {
  const { group } = req.params;
  if (!group) {
    return res.status(400).send({ message: `Invalid group.`});
  }
  
  return res.status(200).send({ id: group });
});


module.exports = router;
