const express = require('express');
const router = express.Router();
const api = require('../lib/dataApi');
const _ = require('lodash');

const funds = require('../public/funds');

const getFundDetails = _.memoize(isin => {
	return _.find(funds, { isin: isin });
});

router.get('/getStats', function (req, res, next) {
	if (!req.query.isin) {
		throw new Error('Required parameter missing: "isin".');
	}

	const fund = getFundDetails(req.query.isin);

	if (!fund) {
		throw new Error('Unknown fund: ' + fund + '.');
	}

	const fee = parseFloat(req.query.fee) || (fund.fee / 100);

	return res.send({
		r: [0.024],
		profitPerCf1000: [4600],
		feePerCf1000: [3423],
	})

	return api.getStats(req.query.isin, fee)
		.then(result => res.send(result))
		.catch(next);
});

router.get('/getComparison', function (req, res, next) {
	if (!req.query.isin) {
		throw new Error('Required parameter missing: "isin".');
	}

	const fee = parseFloat(req.query.fee) || 0.003;
	let indexRatio = parseFloat(req.query.indexRatio);
	if (!_.isFinite(indexRatio)) indexRatio = 0.5;

	return res.send({
		profitPerCf1000: [4600],
		feePerCf1000: [3423],
		r: [0.042]
	})

	return api.getComparison(req.query.isin, indexRatio, fee)
		.then(result => res.send(result))
		.catch(next);
});

router.use('/', (err, req, res, next) => {
	console.error(err);
	console.error(err.stack);

	// recklessly show all errors
	res.status(err.status || 500);
	res.send({
		success: false,
		message: err.message,
	});
});

module.exports = router;

@@ -579,97 +579,8 @@
 );%0A%0A
-%09return res.send(%7B%0A%09%09r: %5B0.024%5D,%0A%09%09profitPerCf1000: %5B4600%5D,%0A%09%09feePerCf1000: %5B3423%5D,%0A%09%7D)%0A%0A
 %09ret
@@ -971,96 +971,8 @@
 5;%0A%0A
-%09return res.send(%7B%0A%09%09profitPerCf1000: %5B4600%5D,%0A%09%09feePerCf1000: %5B3423%5D,%0A%09%09r: %5B0.042%5D%0A%09%7D)%0A%0A
 %09ret

remove test data