export type MatchKeys =
	| 'priceIncreaseValue'
	| 'volumeIncreaseValue'
	| 'cogsReductionValue'
	| 'overheadReductionValue'
	| 'profitImpactOnValuationValue'
	| 'debtorsDaysReductionValue'
	| 'reductionInStockDaysValue'
	| 'increaseInCreditorsDaysValue'
	| 'cashImpactOnValuationValue'
	| 'totalImpactOnValuationValue';

export const matchValueToTitle = (value: MatchKeys) => {
	const matches: Record<MatchKeys, string> = {
		priceIncreaseValue: 'Price Increase',
		volumeIncreaseValue: 'Volume Increase',
		cogsReductionValue: 'CoGS reduction',
		overheadReductionValue: 'Overhead reduction',
		profitImpactOnValuationValue: 'Profit Impact on Valuation',
		debtorsDaysReductionValue: 'Debtors days reduction',
		reductionInStockDaysValue: 'Reduction in Stock days',
		increaseInCreditorsDaysValue: 'Increase in creditors days',
		cashImpactOnValuationValue: 'Cash Impact on Valuation',
		totalImpactOnValuationValue: 'Total Impact on Valuation'
	};
	return matches[value];
};

type FinancialImpactParams = {
	companyType: string | null;
	revenue: string | number;
	grossProfit: string | number;
	cogs: string | number;
	ebit: string | number;
	deferredRevenue: string | number;
	priceIncreaseIndex: string | number;
	volumeIncreaseIndex: string | number;
	cogsIndex: string | number;
	overheadReductionIndex: string | number;
	debtorsDayReduction: string | number;
	reductionWipDays: string | number;
	increaseInDeferredRevenueDays: string | number;
	increaseInCreditorsDays: string | number;
	ebitdaValue: string | number;
};

const calculateImpactOfMultiplierAdjustmentOnValuation = (params: FinancialImpactParams) => {
	const {
		companyType,
		revenue,
		grossProfit,
		cogs,
		ebit,
		deferredRevenue,
		priceIncreaseIndex,
		volumeIncreaseIndex,
		cogsIndex,
		overheadReductionIndex,
		debtorsDayReduction,
		reductionWipDays,
		increaseInDeferredRevenueDays,
		increaseInCreditorsDays,
		ebitdaValue
	} = params;

	// const revenue = +lastFinancialInput.Revenue;
	// const grossProfit = +lastFinancialInput['Gross Profit'];
	// const cogs = +lastFinancialInput.COGS;
	// const ebit = +lastFinancialInput.EBIT;
	const rev = +revenue;
	const grossProf = +grossProfit;
	const cogsVal = +cogs;
	const ebitVal = +ebit;

	const priceIncreaseValue = ((rev * +priceIncreaseIndex) / 100) * +ebitdaValue;

	const volumeIncreaseValue = ((grossProf * +volumeIncreaseIndex) / 100) * +ebitdaValue;

	const cogsReductionValue = ((cogsVal * +cogsIndex) / 100) * +ebitdaValue;

	const overheadReductionValue =
		(((grossProf - ebitVal) * +overheadReductionIndex) / 100) * +ebitdaValue;

	const profitImpactOnValuationValue =
		((rev * +priceIncreaseIndex) / 100 +
			(grossProf * +volumeIncreaseIndex) / 100 +
			(cogsVal * +cogsIndex) / 100 +
			((grossProf - ebitVal) * +overheadReductionIndex) / 100) *
		+ebitdaValue;

	const debtorsDaysReductionValue = (rev / 365) * +debtorsDayReduction;

	const reductionInStockDaysValue =
		companyType === 'All Other'
			? (cogsVal / 365) * +reductionWipDays
			: (rev / 365) * +reductionWipDays;

	const increaseInCreditorsDaysValue = (cogsVal / 365) * +increaseInCreditorsDays;
	const increaseInDeferredRevenueDaysValue =
		(+deferredRevenue / 365) * +increaseInDeferredRevenueDays;

	// const cashImpactOnValuationValue =
	// 	(rev / 365) * +debtorsDayReduction +
	// 	(rev / 365) * +reductionWipDays +
	// 	(cogsVal / 365) * +increaseInCreditorsDays;

	const cashImpactOnValuationValue =
		debtorsDaysReductionValue + reductionInStockDaysValue + increaseInCreditorsDaysValue;

	const totalImpactOnValuationValue = profitImpactOnValuationValue + cashImpactOnValuationValue;

	// const impactValues = [
	// 	{ name: 'priceIncreaseValue', value: priceIncreaseValue },
	// 	{ name: 'volumeIncreaseValue', value: volumeIncreaseValue },
	// 	{ name: 'cogsReductionValue', value: cogsReductionValue },
	// 	{ name: 'overheadReductionValue', value: overheadReductionValue },
	// 	{ name: 'profitImpactOnValuationValue', value: profitImpactOnValuationValue },
	// 	{ name: 'debtorsDaysReductionValue', value: debtorsDaysReductionValue },
	// 	{ name: 'reductionInStockDaysValue', value: reductionInStockDaysValue },
	// 	{ name: 'increaseInCreditorsDaysValue', value: increaseInCreditorsDaysValue },
	// 	{ name: 'cashImpactOnValuationValue', value: cashImpactOnValuationValue },
	// 	{ name: 'totalImpactOnValuationValue', value: totalImpactOnValuationValue }
	// ];

	const impactValueDrivers = [
		{ name: 'priceIncreaseValue', value: priceIncreaseValue },
		{ name: 'volumeIncreaseValue', value: volumeIncreaseValue },
		{ name: 'cogsReductionValue', value: cogsReductionValue },
		{ name: 'overheadReductionValue', value: overheadReductionValue },
		// { name: 'profitImpactOnValuationValue', value: profitImpactOnValuationValue },
		{ name: 'debtorsDaysReductionValue', value: debtorsDaysReductionValue },
		{ name: 'reductionInStockDaysValue', value: reductionInStockDaysValue },
		{ name: 'increaseInDeferredRevenueDaysValue', value: increaseInDeferredRevenueDaysValue },
		{ name: 'increaseInCreditorsDaysValue', value: increaseInCreditorsDaysValue }
		// { name: 'cashImpactOnValuationValue', value: cashImpactOnValuationValue },
		// { name: 'totalImpactOnValuationValue', value: totalImpactOnValuationValue }
	];

	// Sort impact values descending to find top 3
	const topImpacts = impactValueDrivers
		.sort((a, b) => b.value - a.value)
		.slice(0, 3)
		.map((item) => item.name);

	return {
		priceIncreaseValue,
		volumeIncreaseValue,
		cogsReductionValue,
		overheadReductionValue,
		profitImpactOnValuationValue,
		debtorsDaysReductionValue,
		reductionInStockDaysValue,
		increaseInDeferredRevenueDaysValue,
		increaseInCreditorsDaysValue,
		cashImpactOnValuationValue,
		totalImpactOnValuationValue,
		topImpacts
	};
};

export default calculateImpactOfMultiplierAdjustmentOnValuation;
