import dayjs from 'dayjs';

import { INTERVAL } from '../../constants';

const createMainMetrics = ({ metricViews, metricAttentionSpan, metricSearches, interval }) => {
  const head = ['Date', 'No. of Views', 'Attention Span', 'Engagement Score', 'No. of Searches'];

  const views = [...metricViews].reverse();
  const attentionSpan = [...metricAttentionSpan].reverse();
  const searches = [...metricSearches].reverse();

  const body = [];
  views.forEach(({ time, value }, index) => {
    body.push([
      dayjs(time).format(interval === INTERVAL.hours ? 'HH:MM:SS A' : 'DD-MMM-YY'),
      `"${value}"`,
      attentionSpan[index]?.value ? `"${attentionSpan[index]?.value}"` : 'N/A',
      'N/A',
      searches[index]?.value ? `"${searches[index]?.value}"` : 'N/A',
    ]);
  });

  const rows = [head, ...body];

  const csvContent = rows.map((e) => e.join(',')).join('\n');

  return csvContent;
};

export default createMainMetrics;
