const mongoose = require('mongoose');
const moment = require('moment');

const Timelog = mongoose.model('Timelog');

function getLastWeekData(arr) {
  const lastSunday = moment().isoWeekday(0)._d;
  const lastMonday = moment().isoWeekday(-6)._d;

  const filtered = arr.filter(item => {
    const date = new Date(item.started);
    if (moment(date).isBetween(lastMonday, lastSunday, 'day', '[]')) {
      return item;
    }
    return false;
  });
  return filtered;
}

function getTodayData(arr) {
  const today = new Date();
  const filtered = arr.filter(item => {
    const date = new Date(item.started);
    if (moment(date).isSame(today, 'day')) {
      return item;
    }
    return false;
  });
  return filtered;
}

function getThisMonthData(arr) {
  const firstDay = moment().startOf('month');
  const lastday = moment().endOf('month');

  const filtered = arr.filter(item => {
    const date = new Date(item.started);
    if (moment(date).isBetween(firstDay, lastday, 'day', '[]')) {
      return item;
    }
    return false;
  });
  return filtered;
}

function formatData(arr) {
  const newObj = {};

  arr.forEach(item => {
    if (newObj[item.project] === undefined) {
      newObj[item.project] = { time: 0, id: 0 };
      newObj[item.project][item.task] = { taskName: '', time: 0, id: 0 };
      newObj[item.project].time += item.seconds;
      newObj[item.project].id = item.project;
      newObj[item.project][item.task].taskName = item.name;
      newObj[item.project][item.task].id = item.task;
      newObj[item.project][item.task].time += item.seconds;
    } else {
      newObj[item.project].time += item.seconds;
      if (newObj[item.project][item.task] === undefined) {
        newObj[item.project][item.task] = { taskName: '', time: 0, id: 0 };
        newObj[item.project][item.task].taskName = item.name;
        newObj[item.project][item.task].id = item.task;
        newObj[item.project][item.task].time += item.seconds;
      } else {
        newObj[item.project][item.task].time += item.seconds;
      }
    }
  });

  return Object.values(newObj);
}

exports.getLastMonthData = async (req, res) => {
  /*
   We are subtracting here to have a buffer of extra week,
   because we show data for this month, but also for the last week
  */
  const setFirstDay = moment()
    .startOf('month')
    .subtract({ days: 7 });
  const setLastDay = moment().endOf('month');

  const data = await Timelog.find({
    author: req.user._id,
    started: {
      $gte: setFirstDay,
      $lte: setLastDay,
    },
  });
  const lastWeek = getLastWeekData(data);
  const today = getTodayData(data);
  const month = getThisMonthData(data);
  const formattedWeek = formatData(lastWeek);
  const formattedToday = formatData(today);
  const formattedMonth = formatData(month);

  res.send({
    lastWeek: formattedWeek,
    today: formattedToday,
    month: formattedMonth,
  });
};

@@ -123,729 +123,38 @@
 ion 
-getLastWeekData(arr) %7B%0A  const lastSunday = moment().isoWeekday(0)._d;%0A  const lastMonday = moment().isoWeekday(-6)._d;%0A%0A  const filtered = arr.filter(item =%3E %7B%0A    const date = new Date(item.started);%0A    if (moment(date).isBetween(lastMonday, lastSunday, 'day', '%5B%5D')) %7B%0A      return item;%0A    %7D%0A    return false;%0A  %7D);%0A  return filtered;%0A%7D%0A%0Afunction getTodayData(arr) %7B%0A  const today = new Date();%0A  const filtered = arr.filter(item =%3E %7B%0A    const date = new Date(item.started);%0A    if (moment(date).isSame(to
+filterData(arr, 
 day
+1
 , 
-'
 day
-')
+2
 ) %7B%0A
-      return item;%0A    %7D%0A    return false;%0A  %7D);%0A  return filtered;%0A%7D%0A%0Afunction getThisMonthData(arr) %7B%0A  const firstDay = moment().startOf('month');%0A  const lastday = moment().endOf('month');%0A%0A
   co
@@ -265,25 +265,18 @@
 een(
-firstD
+d
 ay
+1
 , 
-last
 day
+2
 , 'd
@@ -1849,35 +1849,136 @@
 last
-Week = getLastWeekData(data
+Sunday = moment().isoWeekday(0)._d;%0A  const lastMonday = moment().isoWeekday(-6)._d;%0A  const firstDay = moment().startOf('month'
 );%0A 
@@ -1993,71 +1993,196 @@
 oday
+Is
  = 
-getTodayData(data);%0A  const month = getThisMonthData(data
+new Date();%0A%0A  const lastWeek = filterData(data, lastMonday, lastSunday);%0A  const today = filterData(data, todayIs, todayIs);%0A  const month = filterData(data, firstDay, setLastDay
 );%0A
+%0A
   co

refactor filtering functions into one