var joi = require('joi');

var gameController = require('./controllers/game.js');
var utils = require('../lib/utils');


module.exports = function (server) {
  server.route({
    method: 'GET',
    path: '/games',
    handler: function (request, reply) {
      gameController.get(request)
      .then(reply)
      .catch(function (err) {
        reply(utils.returnError(err));
      });
    }
  });

  server.route({
    method: 'POST',
    path: '/games',
    handler: function (request, reply) {
      gameController.create(request)
      .then(reply)
      .catch(function (err) {
        reply(utils.returnError(err));
      });
    },
    config: {
      validate: {
        payload: {
          // Game Description is optional.
          description: joi.string().example('Mario Bros. is a sweet adventure game.'),

          // Game URL must start with `https://` or `http://`.
          game_url: joi.string().regex(/^https?:\/\//).required()
            .example('http://nintendo.com/mario-bros/'),

          // Game Name cannot be longer than 150 characters long.
          name: joi.string().max(150).required()
            .example('Mario Bros.'),

          // Game Slug cannot be all digits, all underscores, or all hyphens
          // and must contain only letters, numbers, underscores, and hyphens.
          // TODO: Throw an error if `slug` is already taken.
          slug: joi.string().regex(/^(?!\d*$)(?!_*$)(?!-*$)[\w-]+$/).required()
            .example('mario-bros')
        }
      }
    }
  });

  // TODO: Throw error if neither `/games/{id}` nor `/games/{slug}` resolves.
  server.route({
    method: 'GET',
    path: '/games/{idOrSlug}',
    handler: function (request, reply) {
      gameController.get(request)
      .then(reply)
      .catch(function (err) {
        reply(utils.returnError(err));
      });
    },
    // config: {
    //   validate: {
    //     params: {
    //       // Game Slug cannot be all digits, all underscores, or all hyphens
    //       // and must contain only letters, numbers, underscores, and hyphens.
    //       // TODO: ID *or* slug is required; enforce.
    //       idOrSlug: joi.number().integer().example('9'),

    //       // Game Slug cannot be all digits, all underscores, or all hyphens
    //       // and must contain only letters, numbers, underscores, and hyphens.
    //       // TODO: ID *or* slug is required; enforce.
    //       idOrSlug: joi.string().regex(/^(?!\d*$)(?!_*$)(?!-*$)[\w-]+$/)
    //         .example('mario-bros')
    //     }
    //   }
    // }
  });
};

@@ -151,16 +151,83 @@
 rver) %7B%0A
+  /*%0A  Sample usage:%0A%0A    curl 'http://localhost:4000/games'%0A%0A  */%0A
   server
@@ -460,16 +460,223 @@
 %0A  %7D);%0A%0A
+  /*%0A  Sample usage:%0A%0A    curl -X POST 'http://localhost:4000/games' %5C%0A      -d '%7B%22name%22: %22mario bros%22, %22game_url%22: %22http://nintendo.com%22, %22slug%22: %22mario%22%7D' %5C%0A      -H 'Content-Type: application/json'%0A%0A  */%0A
   server
@@ -1793,16 +1793,130 @@
 %0A  %7D);%0A%0A
+  /*%0A  Sample usage:%0A%0A    curl 'http://localhost:4000/games/1'%0A    curl 'http://localhost:4000/games/mario'%0A%0A  */%0A
   // TOD

add comments of sample usage for existing game API endpoints (fixes #285)