import { isBefore } from 'date-fns';
import { getNow, getYesterday } from '@material-hu/utils/time';

import { type PillsProps } from '@material-hu/components/design-system/Pills';

import { getEndOfDay } from 'src/utils/timeUtils';

import { SessionStatus } from './constants';
import { type SessionBasicInformation, type SessionStatusType } from './types';

export const getSessionStatusType = (
  status: SessionStatusType,
): PillsProps['type'] => {
  switch (status) {
    case SessionStatus.finished:
      return 'highlight';
    case SessionStatus.published:
      return 'success';
    default:
      return 'neutral';
  }
};

export const isOldLocalDate = (
  timeInfo: Pick<SessionBasicInformation, 'localDate' | 'timeZone'>,
) => {
  const { localDate, timeZone } = timeInfo;
  if (!localDate) return false;

  const yesterday = getEndOfDay(getYesterday(timeZone));

  return isBefore(localDate, yesterday);
};

export const isOldStartTime = (
  timeInfo: Pick<SessionBasicInformation, 'startTime' | 'timeZone'>,
) => {
  const { startTime, timeZone } = timeInfo;
  if (!startTime) return false;

  const now = getNow(timeZone);

  return isBefore(startTime, now);
};

export const isOldSession = (
  timeInfo: Pick<
    SessionBasicInformation,
    'startTime' | 'localDate' | 'timeZone'
  >,
) => {
  return isOldLocalDate(timeInfo) || isOldStartTime(timeInfo);
};
