/*!
 * q - http
 * Copyright (c) 2011 LearnBoost <tj@learnboost.com>
 * MIT Licensed
 */

/**
 * Module dependencies.
 */

var express = require('express');

// setup

var app        = express()
  , bodyParser = require('body-parser')
  , provides   = require('./middleware/provides')
  , routes     = require('./routes')
  , json       = require('./routes/json')
  , util       = require('util')
  , nib        = require('nib');


// middlewares

app.use(express.static(__dirname + '/public'));

// JSON api

app.get('/stats', provides('json'), json.stats);
app.get('/job/search', provides('json'), json.search);
app.get('/jobs/:from..:to/:order?', provides('json'), json.jobRange);
app.get('/jobs/:type/:state/:from..:to/:order?', provides('json'), json.jobTypeRange);
app.get('/jobs/:type/:state/stats', provides('json'), json.jobTypeStateStats);
app.get('/jobs/:state/:from..:to/:order?', provides('json'), json.jobStateRange);
app.get('/job/types', provides('json'), json.types);
app.get('/job/:id', provides('json'), json.job);
app.get('/job/:id/log', provides('json'), json.log);
app.put('/job/:id/state/:state', provides('json'), json.updateState);
app.put('/job/:id/priority/:priority', provides('json'), json.updatePriority);
app.delete('/job/:id', provides('json'), json.remove);
app.post('/job', provides('json'), bodyParser.json(), json.createJob);
app.get('/inactive/:id', provides('json'), json.inactive);

// app.get('/active', routes.jobs('active'));
// app.get('/inactive', routes.jobs('inactive'));
// app.get('/failed', routes.jobs('failed'));
// app.get('/complete', routes.jobs('complete'));
// app.get('/delayed', routes.jobs('delayed'));

// expose the app
module.exports = function(Queue, Job) {
  app.queue = Queue.createQueue();
  app.Job = Job;
  return app;
}