import { ICustomRow } from '../interfaces/custom-row.interface';
import { ECollaterals } from '../enums/collaterals.enum';
import { EValidators } from '../enums/validators.enums';
import { EDataTypes } from '../enums/data-types.enum';

export let COLLATERALS: { [p: string]: ICustomRow[] };
COLLATERALS = {
  [ECollaterals.INVENTORY]: [
    {
      db_title: 'bbcDate',
      title: 'BBC date',
      required: false,
      items: [],
      hidden: true,
      validators: [EValidators.NOT_EMPTY, EValidators.IS_DATE],
      dataType: EDataTypes.DATE,
    },
    {
      db_title: 'skuDate',
      title: 'SKU date',
      required: false,
      items: [],
      validators: [EValidators.IS_DATE],
      isDate: true,
      dataType: EDataTypes.DATE,
    },
    {
      db_title: 'sku',
      title: 'SKU',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
      dataType: EDataTypes.STRING,
    },
    { db_title: 'lotNumber', title: '#LOT', required: false, items: [], dataType: EDataTypes.STRING },
    { db_title: 'category', title: 'Category', required: false, items: [], dataType: EDataTypes.STRING },
    {
      db_title: 'qty',
      title: 'Qty',
      required: false,
      items: [],
      validators: [EValidators.NOT_EMPTY, EValidators.IS_NUMBER],
      dataType: EDataTypes.NUMBER,
    },
    {
      db_title: 'unitCost',
      title: 'Unit cost',
      required: false,
      items: [],
      validators: [EValidators.IS_NUMBER],
      dataType: EDataTypes.NUMBER,
    },
    {
      db_title: 'value',
      title: 'Value',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY, EValidators.IS_NUMBER],
      dataType: EDataTypes.NUMBER,
    },
    {
      db_title: 'expiryDate',
      title: 'Expiry date',
      required: false,
      items: [],
      validators: [EValidators.IS_DATE],
      isDate: true,
      dataType: EDataTypes.DATE,
    },
    { db_title: 'location', title: 'Location', required: false, items: [], dataType: EDataTypes.STRING },
    {
      db_title: 'skuDescription1',
      title: 'SKU Description 1',
      required: false,
      items: [],
      dataType: EDataTypes.STRING,
    },
    {
      db_title: 'skuDescription2',
      title: 'SKU Description 2',
      required: false,
      items: [],
      dataType: EDataTypes.STRING,
    },
    {
      db_title: 'skuDescription3',
      title: 'SKU Description 3',
      required: false,
      items: [],
      dataType: EDataTypes.STRING,
    },
    {
      db_title: 'detail1',
      title: 'Detail 1',
      required: false,
      items: [],
      dataType: EDataTypes.STRING,
    },
    {
      db_title: 'detail2',
      title: 'Detail 2',
      required: false,
      items: [],
      dataType: EDataTypes.STRING,
    },
    {
      db_title: 'detail3',
      title: 'Detail 3',
      required: false,
      items: [],
      dataType: EDataTypes.STRING,
    },
  ],
  [ECollaterals.RECEIVABLE]: [
    {
      db_title: 'bbcDate',
      title: 'BBC date',
      required: false,
      items: [],
      hidden: true,
      validators: [EValidators.NOT_EMPTY, EValidators.IS_DATE],
    },
    {
      db_title: 'invoiceNumber',
      title: 'Invoice number',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    },
    {
      db_title: 'invoiceDate',
      title: 'Invoice date',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY, EValidators.IS_DATE],
      isDate: true,
    },
    {
      db_title: 'customerTitle',
      title: 'Customer title',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    },
    {
      db_title: 'invoiceAmount',
      title: 'Invoice amount',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY, EValidators.IS_NUMBER],
    },
    {
      db_title: 'originalAmount',
      title: 'Original amount',
      required: false,
      items: [],
      validators: [EValidators.IS_NUMBER],
    },
    {
      db_title: 'dueDate',
      title: 'Due date',
      required: false,
      items: [],
      validators: [EValidators.IS_DATE],
      isDate: true,
    },
    {
      db_title: 'invoiceDetail1',
      title: 'Invoice detail 1',
      required: false,
      items: [],
    },
    {
      db_title: 'invoiceDetail2',
      title: 'Invoice detail 2',
      required: false,
      items: [],
    },
    {
      db_title: 'invoiceDetail3',
      title: 'Invoice detail 3',
      required: false,
      items: [],
    },
  ],
  [ECollaterals.ACCOUNT_PAYABLE]: [
    {
      db_title: 'bbcDate',
      title: 'BBC date',
      required: false,
      items: [],
      validators: [EValidators.NOT_EMPTY, EValidators.IS_DATE],
    },
    {
      db_title: 'apDate',
      title: 'AP date',
      required: false,
      items: [],
      validators: [EValidators.NOT_EMPTY, EValidators.IS_DATE],
      isDate: true,
    },
    { db_title: 'poNumber', title: 'PO number', required: false, items: [] },
    { db_title: 'customerName', title: 'Customer name', required: false, items: [] },
    {
      db_title: 'amount',
      title: 'Amount',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY, EValidators.IS_NUMBER, EValidators.NUMBER_GT_NULL],
    },
    { db_title: 'otherInformation1', title: 'Other Information 1', required: false, items: [] },
    { db_title: 'otherInformation2', title: 'Other Information 2', required: false, items: [] },
    { db_title: 'otherInformation3', title: 'Other Information 3', required: false, items: [] },
  ],
  [ECollaterals.SALES]: [
    {
      db_title: 'bbcDate',
      title: 'BBC date',
      required: false,
      items: [],
      validators: [EValidators.NOT_EMPTY, EValidators.IS_DATE],
    },
    {
      db_title: 'sku',
      title: 'SKU',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    },
    {
      db_title: 'skuName',
      title: 'SKU Name',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    },
    {
      db_title: 'unitsSold',
      title: 'Units sold',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY, EValidators.IS_NUMBER, EValidators.NUMBER_GT_NULL],
    },
    {
      db_title: 'unitsCost',
      title: 'Units cost',
      required: true,
      items: [],
      validators: [EValidators.IS_NUMBER],
    },
    {
      db_title: 'unitSalePrice',
      title: 'Unit sale price',
      required: true,
      items: [],
      validators: [EValidators.IS_NUMBER],
    },
  ],
  [ECollaterals.LOAN_TRANSACTIONS]: [
    {
      db_title: 'date',
      title: 'Transaction date',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY, EValidators.IS_DATE],
      isDate: true,
    },

    {
      db_title: 'ledgerAccountCode',
      title: 'Ledger account code',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    },
    {
      db_title: 'amount',
      title: 'Amount',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY, EValidators.IS_NUMBER],
    },
    {
      db_title: 'description',
      title: 'Description',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    },
    {
      db_title: 'reference',
      title: 'Reference',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    },
    {
      db_title: 'customerId',
      title: 'Customer ID',
      required: false,
      items: [],
      validators: [],
    },
    {
      db_title: 'bankAccountNumber',
      title: 'Bank account number',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    },
  ],
  [ECollaterals.UPLOADED_BANK_TRANSACTIONS]: [
    {
      db_title: 'date',
      title: 'Transaction date',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY, EValidators.IS_DATE],
      isDate: true,
    },

    {
      db_title: 'accountNumber',
      title: 'Account number',
      required: false,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    },
    {
      db_title: 'amount',
      title: 'Amount',
      required: false,
      items: [],
      validators: [EValidators.IS_NUMBER],
    },
    {
      db_title: 'credit',
      title: 'Credit',
      required: false,
      items: [],
      validators: [EValidators.IS_NUMBER],
    },
    {
      db_title: 'debit',
      title: 'Debit',
      required: false,
      items: [],
      validators: [EValidators.IS_NUMBER],
    },
    {
      db_title: 'transactionType',
      title: 'Transaction type',
      required: false,
      items: [],
      validators: [],
    },
    {
      db_title: 'reference',
      title: 'Reference',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    },
    {
      db_title: 'detail1',
      title: 'Detail 1',
      required: false,
      items: [],
      validators: [],
    },
    {
      db_title: 'detail2',
      title: 'Detail 2',
      required: false,
      items: [],
      validators: [],
    },
    {
      db_title: 'detail3',
      title: 'Detail 3',
      required: false,
      items: [],
      validators: [],
    },
  ],
  [ECollaterals.QUICKBOOKS]: [
    {
      db_title: 'accountCode',
      title: 'Account Code',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    },
    {
      db_title: 'fullName',
      title: 'Full Name',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    }, {
      db_title: 'description',
      title: 'Description',
      required: true,
      items: [],
      validators: [EValidators.NOT_EMPTY],
    },
  ],
};

export const convertIntoDates = (item, collateralType: ECollaterals) => {
  return Object.keys(item).reduce((_, key) => {
    const foundColumn = COLLATERALS[collateralType].find((c) => c.db_title === key);
    if (!!foundColumn && !!foundColumn.validators && foundColumn.validators.includes(EValidators.IS_DATE)) {
      if (!!item[key]) {
        const newDate = new Date(item[key].toString().trim().replace(' ', ''));
        if (newDate instanceof Date && !isNaN(newDate.valueOf())) {
          item[key] = newDate;
        }
      }
    }
    return item;
  }, item);
};
