var settings = require('../settings');
var utils = require('../utils');
var _ = require('underscore');
var path = require('path');

var db = require('./db');
var log = require('../log');

var make_defaults = utils.make_class({
  init: function(){
    var self = this;
    self.tags =  [];
    self.links =  {};
    self.environments =  [];
    self.metadata =  {};
    self.related_apis =  {};
    self.dependent_services =  [];
    self.events =  [];
    self.kpi_spec =  '';

    self.errors =  [];
    // By convention, middlewares will populate this fields,
    // if the related_apis exist, with {'error': X, 'data': X}
    self.pager_duty =  null;
    self.version_one =  null;
    self.github =  null;
    self.new_relic =  null;
    self.dreadnot =  null;
  }
});

/**
 * If req.params.project, assigns req.devops to loaded devops object
 */
module.exports = function load_fixtures(cb) {
  var extcreds;
  log.log("Loading fixtures...");

  db.get_projects(undefined, function (err, results) {
    _.each(results, function(row){
      var api_name;
      var api_obj;
      var devops_json = row.devops_json ? JSON.parse(row.devops_json): {};
      var project_name = row.name;

      // fill in missing optional fields
      _.defaults(devops_json, new make_defaults());

      // fill in __external__ related api creds from local settings
      // if they exist
      // TODO: move external_creds into DB
      if (settings.external_creds && settings.external_creds[project_name]) {
        extcreds = settings.external_creds[project_name];
        for (api_name in devops_json.related_apis){
          api_obj = devops_json.related_apis[api_name];
          if (extcreds[api_name]){
            _.extend(api_obj, extcreds[api_name]);
          }
          devops_json[api_name] = api_obj;
        }
      }
      PROJECTS[project_name] = row;
      PROJECTS[project_name].devops = devops_json;
    });

    cb(null, null);
  });
};

@@ -893,24 +893,8 @@
 ) %7B%0A
-  var extcreds;%0A
   lo
@@ -1406,133 +1406,20 @@
 if (
-settings.external_creds && settings.external_creds%5Bproject_name%5D) %7B%0A        extcreds = settings.external_creds%5Bproject_name%5D;
+row.creds) %7B
 %0A   
@@ -1537,19 +1537,20 @@
     if (
-ext
+row.
 creds%5Bap
@@ -1589,19 +1589,20 @@
 pi_obj, 
-ext
+row.
 creds%5Bap

Load external creds from db instead of settings