'use strict';

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

//These routes should be activated under /test/fetch so that they can be called by the test.html
//when testing your fetch wrappers doFetchRequest, doJSONRequest found in the public/js/fetch.js 
//Add the following to your app.js:
//app.use('/test/fetch', routers.fetch_tests);

router.get('/', function(req, res, next) {
  if(req.accepts('json')) {
    res.json({
      text: 'GET Working'
    })
  } else {
    res.status(200).end('GET Working')
  }
});

router.all('/json/ok', function(req, res, next) {
    res.json({
      text: 'ok'
    });
});


router.delete('/', function(req, res, next) {
  if(req.accepts('json')) {
    res.json({
      status: 204,
      text: 'DELETE Working'
    })
  } else {
    res.status(204).end()
  }
});

router.get('/redirect', function(req, res, next) {
  res.redirect('redirected')
});

router.get('/redirected', function(req, res, next) {
  res.status(200).end('Redirect Works')
});

router.post('/new', function(req, res, next) {
  if(req.accepts('json')) {
    res.type('json');
    res.set('Location', '24');
    res.status(201).json({
      text: 'POST Working',
      body: req.body
    })
  } else {
    res.set('Location', '42');
    res.status(201).end('POST Working\n'+req.body)
  }
});

router.put('/echo', function(req, res, next) {
  if(req.accepts('json')) {
    res.json({
      text: 'PUT Working',
      body: req.body
    })
  } else {
    res.end(req.body)
  }
});

module.exports = router;
