import { TQueryParams } from '../../models/QueryResult.model';
import { addHeaderToData } from '../../helpers/excel.helper';
import { getAverageInvoiceSize, getAverageOver } from '../../db/receivables.db';

export const getAverageInvoiceFull = async (params: TQueryParams) => {
  const dateAll = await getAverageInvoiceData(params);
  const dataTop = await getAverageOverData(params);

  const headerAll = getAverageInvoiceFullHeader('all');
  const headerTop = getAverageInvoiceFullHeader('top');

  const tableAll = addHeaderToData(headerAll, dateAll);
  const tableTop = addHeaderToData(headerTop, dataTop);

  return { 'all': tableAll, 'top': tableTop };
};

export const getAverageInvoiceData = async (params: TQueryParams) => {
  const { borrowerId } = params;
  return getAverageInvoiceSize(borrowerId.toString());
};

export const getAverageOverData = async (params: TQueryParams) => {
  const { borrowerId } = params;
  return getAverageOver(borrowerId.toString());
};

const getAverageInvoiceFullHeader = (type: 'all' | 'top') => {
  const headerMap = {
    'all': {
      bbcDate: 'BBC date',
      totalInvoices: 'Number of Invoices',
      totalAmount: 'Total Invoices',
      averageInvoiceSize: 'Average Invoice',
    },
    'top': {
      bbcDate: 'BBC date',
      invoiceNumber: 'Invoice number',
      invoiceDate: 'Invoice date',
      customerTitle: 'Customer name',
      invoiceAmount: 'Invoice amount',
    },
  };
  return headerMap[type];
};
