var express = require("express");
var MailChimpAPI = require("mailchimp").MailChimpAPI;

var apiKey = "d16d715e2229f247a3ec9267c71b816a-us7";

var api;
try {
	api = new MailChimpAPI(apiKey, {
		version : "2.0"
	});
} catch (error) {
	return console.log(error.message);
}

module.exports = function(routeConfigs) {
	if (!routeConfigs) {
		return;
	}

	if (!(routeConfigs instanceof Array)) {
		return;
	}

	var router = express.Router();

	routeConfigs.forEach(function(routeConfig) {
		router.post(routeConfig.route, function(req, res) {
			api.call("lists", "subscribe", {id: routeConfig.listId, email: {email: req.body.email}}, function(err, result) {
				res.send({err: err, result: result});
			});
		});
	});

	return router;
};