import { berlinParts } from '#shared/staffing' import type { Point, Shift, StaffingHour, StaffingResponse } from '#shared/types' import { formatShortDate, formatWeekday, formatWeekdayShort } from '~/utils/format' export interface PlanDay { date: string dayNumber: string month: string weekday: string weekdayLong: string shifts: Shift[] hours: StaffingHour[] staffHours: number peakStaff: number peakFootfall: number peakHour: number cost: number overHours: number underHours: number } export function maxOf(items: T[], score: (item: T) => number): T | undefined { let best: T | undefined for (const item of items) { if (!best || score(item) > score(best)) best = item } return best } export function peakShiftOf(plan: StaffingResponse | null | undefined): Shift | undefined { return plan ? maxOf(plan.shifts, (shift) => shift.staff) : undefined } export function peakHourOf(plan: StaffingResponse | null | undefined): StaffingHour | undefined { return plan ? maxOf(plan.hours, (hour) => hour.footfall) : undefined } export function totalStaffHoursOf(plan: StaffingResponse | null | undefined): number { return plan ? plan.hours.reduce((sum, hour) => sum + hour.scheduled_staff, 0) : 0 } export function savingOf(plan: StaffingResponse | null | undefined): number { if (!plan) return 0 return Math.max(0, plan.summary.flat_schedule_cost_eur - plan.summary.cost_eur) } export function savingPctOf(plan: StaffingResponse | null | undefined): number { if (!plan || plan.summary.flat_schedule_cost_eur <= 0) return 0 return (savingOf(plan) / plan.summary.flat_schedule_cost_eur) * 100 } export function planDays(plan: StaffingResponse | null | undefined): PlanDay[] { if (!plan) return [] const shiftsByDate: Record = {} for (const shift of plan.shifts) (shiftsByDate[shift.date] ??= []).push(shift) const hoursByDate: Record = {} for (const hour of plan.hours) { const date = berlinParts(hour.t).date ;(hoursByDate[date] ??= []).push(hour) } const dates = [...new Set([...Object.keys(shiftsByDate), ...Object.keys(hoursByDate)])].sort() const costPerHour = plan.assumptions.labor_cost_eur_h return dates.map((date) => { const shifts = (shiftsByDate[date] ?? []).slice().sort((a, b) => a.start.localeCompare(b.start)) const hours = (hoursByDate[date] ?? []).slice().sort((a, b) => a.t.localeCompare(b.t)) const peakHour = maxOf(hours, (hour) => hour.footfall) const staffHours = hours.reduce((sum, hour) => sum + hour.scheduled_staff, 0) const [dayNumber = '', month = ''] = formatShortDate(date).split(' ') return { date, dayNumber, month, weekday: formatWeekdayShort(date), weekdayLong: formatWeekday(date), shifts, hours, staffHours, peakStaff: Math.max(0, ...hours.map((hour) => hour.scheduled_staff), ...shifts.map((shift) => shift.staff)), peakFootfall: peakHour?.footfall ?? 0, peakHour: peakHour ? berlinParts(peakHour.t).hour : 0, cost: staffHours * costPerHour, overHours: hours.filter((hour) => hour.coverage === 'over').length, underHours: hours.filter((hour) => hour.coverage === 'under').length, } }) } export function planPulsePoints(plan: StaffingResponse | null | undefined): Point[] { return (plan?.hours ?? []).map((hour) => ({ t: hour.t, v: hour.footfall })) } export function numberWord(value: number): string { const words = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten'] return words[value] ?? String(value) } export function shiftLabel(shift: Shift): string { return `${shift.start}–${shift.end}` } export function coverWord(value: number, singular: string, plural = `${singular}s`): string { return `${value} ${value === 1 ? singular : plural}` }