import {
  DASHBOARD_QUICK_ACTIONS,
  DASHBOARD_UPCOMING_EVENTS_LIMIT,
} from '@/shared/constants/dashboard';
import type { DashboardQuickAction } from '@/shared/constants/dashboard';
import type {
  UserRole,
  ZoneColor,
} from '@/shared/types/app';
import type { CommunicationEventDto } from '@/shared/types/communications';
import type { DashboardEncouragingQuote } from '@/shared/types/dashboard';

export function getDashboardGreeting(date: Date): string {
  const hour = date.getHours();

  if (hour < 12) {
    return 'Good Morning';
  }

  if (hour < 17) {
    return 'Good Afternoon';
  }

  return 'Good Evening';
}

export function getDashboardDayOfYear(date: Date): number {
  const yearStart = new Date(date.getFullYear(), 0, 0).getTime();

  return Math.floor((date.getTime() - yearStart) / 86400000);
}

export function selectDashboardQuote(
  quotes: readonly DashboardEncouragingQuote[],
  date: Date,
): DashboardEncouragingQuote | null {
  if (quotes.length === 0) {
    return null;
  }

  return quotes[getDashboardDayOfYear(date) % quotes.length];
}

export function toDashboardZoneColor(value: string | null): ZoneColor | null {
  if (value === 'blue' || value === 'green' || value === 'yellow' || value === 'red') {
    return value;
  }

  return null;
}

export function selectDashboardActiveZone(
  localZone: string | null,
  savedZone: ZoneColor | null,
): ZoneColor | null {
  return toDashboardZoneColor(localZone) ?? savedZone;
}

export function selectDashboardUpcomingEvents(
  events: readonly CommunicationEventDto[],
): readonly CommunicationEventDto[] {
  return events.slice(0, DASHBOARD_UPCOMING_EVENTS_LIMIT);
}

export function selectDashboardQuickActions(userRole: UserRole): readonly DashboardQuickAction[] {
  return DASHBOARD_QUICK_ACTIONS.filter((action) => !action.hiddenForRoles?.includes(userRole));
}
