import { ReportType } from "../types/types";

export const getTimeRange = (reportType: ReportType): [number, number] => {
  const today = new Date();
  let startTimestamp: number;
  let endTimestamp: number;

  switch (reportType) {
    case ReportType.WEEKLY_REPORT:
      {
        // 本周周报
        const startOfWeek = new Date(
          today.setDate(
            today.getDate() - today.getDay() + (today.getDay() === 0 ? -6 : 1),
          ),
        );
        startOfWeek.setHours(0, 0, 0, 0);
        startTimestamp = Math.floor(startOfWeek.getTime() / 1000);
        endTimestamp = Math.floor(Date.now() / 1000);
      }
      break;
    case ReportType.LAST_WEEK_REPORT: // 上周周报
      {
        const lastWeek = new Date(today.setDate(today.getDate() - 7));
        const startOfLastWeek = new Date(
          lastWeek.setDate(
            lastWeek.getDate() -
              lastWeek.getDay() +
              (lastWeek.getDay() === 0 ? -6 : 1),
          ),
        );
        startOfLastWeek.setHours(0, 0, 0, 0);
        const endOfLastWeek = new Date(startOfLastWeek);
        endOfLastWeek.setDate(startOfLastWeek.getDate() + 6);
        startTimestamp = Math.floor(startOfLastWeek.getTime() / 1000);
        endTimestamp = Math.floor(endOfLastWeek.getTime() / 1000);
      }
      break;
    case ReportType.DAILY_REPORT: // 日报
      {
        const startOfDay = new Date(today.setHours(0, 0, 0, 0));
        startTimestamp = Math.floor(startOfDay.getTime() / 1000);
        endTimestamp = Math.floor(Date.now() / 1000);
      }
      break;

    case ReportType.YESTERDAY_REPORT: // 昨日日报
      {
        const yesterday = new Date(today.setDate(today.getDate() - 1));
        const startOfYesterday = new Date(yesterday.setHours(0, 0, 0, 0));
        const endOfYesterday = new Date(yesterday.setHours(23, 59, 59, 999));
        startTimestamp = Math.floor(startOfYesterday.getTime() / 1000);
        endTimestamp = Math.floor(endOfYesterday.getTime() / 1000);
      }
      break;
  }

  return [startTimestamp, endTimestamp];
};
