var _ = require('lodash');

var db = require('../../db');
var gamelib = require('../../lib/game');


module.exports = function(server) {
    // Sample usage:
    // % curl 'http://localhost:5000/game/mario-bros/detail'
    server.get({
        url: '/game/:slug/detail',
        swagger: {
            nickname: 'detail',
            notes: 'Specific details and metadata about a game',
            summary: 'Game Details'
        }
    }, db.redisView(function(client, done, req, res, wrap) {
        var GET = req.params;
        var slug = GET.slug;

        if (!slug) {
            res.json(400, {error: 'bad_game'});
            done();
            return;
        }

        gamelib.getGameFromSlug(client, slug, function(err, game) {
            if (!game) {
                res.json(500, {error: 'db_error'});
            } else {
                res.json(game);
            }
            done();
        });
    }));
};

@@ -92,16 +92,54 @@
 game');%0A
+var user = require('../../lib/user');%0A
 %0A%0Amodule
@@ -454,16 +454,169 @@
 etails'%0A
+        %7D,%0A        validation: %7B%0A            _user: %7B%0A                description: %22A user's SSA token%22,%0A                isRequired: false%0A            %7D%0A
         
@@ -736,16 +736,48 @@
 ET.slug;
+%0A        var email = req._email;
 %0A%0A      
@@ -1076,57 +1076,1014 @@
     
-res.json(game);%0A            %7D%0A            done();
+game%5B'purchased'%5D = true;%0A                if (email) %7B%0A                    user.getUserIDFromEmail(client, email, function(err, id) %7B%0A                        if (err) %7B%0A                            res.json(game);%0A                            done();%0A                        %7D%0A                        client.sismember('gamesPurchased:' + id, slug, function(err, resp) %7B%0A                            console.log(slug);%0A                            console.log(id);%0A                            console.log(err);%0A                            console.log(resp);%0A                            if (!err && !resp) %7B%0A                                // game has not been purchased%0A                                game%5B'purchased'%5D = false;%0A                            %7D%0A                            res.json(game);%0A                            done();%0A                        %7D);%0A                    %7D);%0A                %7D else %7B%0A                    res.json(game);%0A                    done();%0A                %7D%0A            %7D
 %0A   

add purchased flag in game/detail request, fixes issue #144