var _ = require('lodash');
var P = require('bluebird');
var validate = require('validate-npm-package-name');
var npa = require('npm-package-arg');
var PackageModel = require("../models/package");

var DEPENDENCY_TTL = 5 * 60; // 5 minutes

exports.show = function(request, reply) {
  var name = request.packageName;
  var context = {
    title: name
  };
  var loggedInUser = request.loggedInUser;
  var Download = require("../models/download").new({
    request: request,
    cache: require("../lib/cache")
  });
  var Package = PackageModel.new(request);

  request.logger.info('get package: ' + name);

  var actions = {
    package: Package.get(name),
    dependents: Package.list({
      dependency: name,
      limit: 50
    }, DEPENDENCY_TTL),
    downloads: Download.getAll(name),
  };

  P.props(actions)
    .then(function(results) {
      var pkg = results.package;
      pkg.dependents = results.dependents;
      if (pkg.name[0] != '@') {
        pkg.downloads = results.downloads;
      }

      if (pkg && pkg.time && pkg.time.unpublished) {
        request.logger.info('package is unpublished: ' + name);
        reply.view('package/unpublished', context).code(404);
        return;
      }

      if (_.get(pkg, 'dependents.results.length')) {
        pkg.numMoreDependents = pkg.dependentCount - pkg.dependents.results.length;
      }

      pkg.isStarred = Boolean(loggedInUser)
        && Array.isArray(pkg.stars)
        && pkg.stars.indexOf(loggedInUser.name) > -1;

      pkg.isCollaboratedOnByUser = Boolean(loggedInUser)
        && (typeof pkg.collaborators === "object")
        && (loggedInUser.name in pkg.collaborators);

      pkg.hasStats = pkg.downloads || (pkg.bugs && pkg.bugs.url) || (pkg.pull_requests && pkg.pull_requests.url);

      context.package = pkg;
      return reply.view('package/show', context);

    })
    .catch(function(err) {
      // unpaid collaborator
      if (err.statusCode === 402) {
        reply.redirect('/settings/billing?package=' + name);
        return;
      }

      if (err.statusCode === 404) {
        var pkg = npa(name);
        pkg.available = false;

        if (!validate(name).validForNewPackages) {
          context.package = pkg;
          reply.view('errors/package-not-found', context).code(400);
          return;
        }

        if (pkg.scope) {
          pkg.owner = pkg.scope.slice(1);
          if (loggedInUser) {
            if (pkg.owner === loggedInUser.name) {
              pkg.available = true;
            } else {
              pkg.unavailableToLoggedInUser = true;
            }
          } else {
            pkg.unavailableToAnonymousUser = true;
          }
        } else {
          pkg.available = true;
        }

        context.package = pkg;
        reply.view('errors/package-not-found', context).code(404);
        return;
      }

      request.logger.error(err);
      reply.view('errors/internal', context).code(500);
      return;
    });
};

exports.update = function(request, reply) {
  PackageModel.new(request).update(request.packageName, request.payload.package)
    .then(function(pkg) {
      return reply({
        package: pkg
      });
    })
    .catch(function(err) {
      request.logger.error(err);
      return reply(err);
    });
};

@@ -188,16 +188,63 @@
 ckage%22);
+%0Avar feature = require('../lib/feature-flags');
 %0A%0Avar DE
@@ -667,21 +667,30 @@
  = %7B
+%7D;
 %0A  
-  
+actions.
 package
-:
+ =
  Pac
@@ -703,22 +703,28 @@
 et(name)
-,%0A    
+;%0A  actions.
 dependen
@@ -725,17 +725,18 @@
 pendents
-:
+ =
  Package
@@ -747,18 +747,16 @@
 t(%7B%0A    
-  
 dependen
@@ -769,18 +769,16 @@
 me,%0A    
-  
 limit: 5
@@ -781,18 +781,16 @@
 t: 50%0A  
-  
 %7D, DEPEN
@@ -799,22 +799,56 @@
 NCY_TTL)
-,%0A    
+;%0A  if (!feature('npmo')) %7B%0A    actions.
 download
@@ -848,17 +848,18 @@
 ownloads
-:
+ =
  Downloa
@@ -876,14 +876,13 @@
 ame)
-,
+;
 %0A  %7D
-;
 %0A%0A  

Remove download counts from npmo