/**
 * @package admiral
 * @author Andrew Munsell <andrew@wizardapps.net>
 * @copyright 2015 WizardApps
 */

var Promise = require('bluebird');

exports = module.exports = function(app, config, Applications, Services) {
    /**
     * Get the applications
     */
    app.get('/v1/applications', function(req, res) {
        Promise.all([Applications.all(), Services.all()])
            .spread(function(applications, services) {
                for(var i = 0; i < applications.length; i++) {
                    applications[i].services = services.filter(function(service) {
                        return service.application == applications[i].id
                    });
                }

                return applications;
            })
            .then(function(applications) {
                res
                    .header('Cache-Control', 'private, max-age=60')
                    .json(applications);
            })
            .catch(function(err) {
                res
                    .status(500)
                    .json({
                        error: 500,
                        message: 'There was a problem retrieving the list of applications.'
                    });
            })
            .finally(function() {
                res.end();
            });
    });

    /**
     * Create a new application
     */
    app.post('/v1/applications', function(req, res) {
        Applications.create(req.body)
            .then(function(application) {
                res
                    .json(application);
            })
            .catch(function(errors) {
                return res
                    .status(400)
                    .json(errors)
                    .end();
            });
    });

    /**
     * Get information for the specified application
     */
    app.get('/v1/applications/:id', function(req, res) {
            Applications.get(req.params.id)
                .then(function(application) {
                    return Promise.all([ application, Services.all() ]);
                })
                .spread(function(application, services) {
                    application.services = services.filter(function(service) {
                        return application.id == service.application;
                    });

                    return application;
                })
                .then(function(application) {
                    if(application == null) {
                        return res
                            .status(404)
                            .json({
                                error: 404,
                                message: 'The specified application does not exist.'
                            });
                    }

                    return res
                        .header('Cache-Control', 'private, max-age=60')
                        .json(application);
                })
                .catch(function(err) {
                    return res
                        .status(500)
                        .json({
                            error: 500,
                            message: 'There was a problem retrieving the application.'
                        })
                })
                .finally(function() {
                    res.end();
                });
    });

    /**
     * Get the services for the specified application
     */
    app.get('/v1/applications/:id/services', function(req, res) {
        config.getAsync('/services', { recursive: true })
            .spread(function(result) {
                return (result.node.nodes || [] ).map(function(node) {
                    return JSON.parse(node.value);
                });
            })
            .then(function(services) {
                return services.filter(function(service) {
                    return service.application == req.params.id;
                });
            })
            .then(function(services) {
                res
                    .header('Cache-Control', 'private, max-age=60')
                    .json(services);
            })
            .catch(function(err) {
                if(err.errorCode == 100 && err.error.cause.indexOf('/services') < 0) {
                    res
                        .status(404)
                        .json({
                            error: 404,
                            message: 'The specified application doesn\'t exist.'
                        });
                } else {
                    res
                        .status(500)
                        .json({
                            error: 500,
                            message: 'There was a problem retrieving the list of services.'
                        });
                }
            })
            .finally(function() {
                res.end();
            });
    });

    /**
     * Destroy the specified application
     */
    app.delete('/v1/applications/:id', function(req, res) {
        Applications.del(req.params.id)
            .then(function(result) {
                if(result) {
                    res
                        .json({
                            ok: true
                        });
                } else {
                    res
                        .status(500)
                        .json({
                            error: 500,
                            message: 'There was a problem deleting the specified application.'
                        });
                }
            })
            .catch(function(err) {
                res
                    .status(404)
                    .json({
                        error: 404,
                        message: 'The specified application could not be found.'
                    });
            })
            .finally(function() {
                res.end();
            });
    });
};

exports['@singleton'] = true;
exports['@require'] = ['app', 'config', 'models/Applications', 'models/Services'];
@@ -3446,252 +3446,21 @@
     
-config.getAsync('/services', %7B recursive: true %7D)%0A            .spread(function(result) %7B%0A                return (result.node.nodes %7C%7C %5B%5D ).map(function(node) %7B%0A                    return JSON.parse(node.value);%0A                %7D);%0A            %7D
+Services.all(
 )%0A  
@@ -3573,32 +3573,49 @@
           return
+ req.params.id ==
  service.applica
@@ -3622,25 +3622,8 @@
 tion
- == req.params.id
 ;%0A  
@@ -3889,374 +3889,12 @@
     
-if(err.errorCode == 100 && err.error.cause.indexOf('/services') %3C 0) %7B%0A                    res%0A                        .status(404)%0A                        .json(%7B%0A                            error: 404,%0A                            message: 'The specified application doesn%5C't exist.'%0A                        %7D);%0A                %7D else %7B%0A                    res%0A    
+res%0A
     
@@ -3914,36 +3914,32 @@
    .status(500)%0A
-    
                 
@@ -3966,36 +3966,32 @@
                 
-    
 error: 500,%0A    
@@ -4002,36 +4002,32 @@
                 
-  
-  
 message: 'There 
@@ -4098,33 +4098,11 @@
     
-    %7D);%0A                %7D
+%7D);
 %0A   

Use new service model helper when retrieving services for an application