import type { Duration, DurationFitStatus } from './types'

const REQUESTED_DURATION_MINUTES: Record<Duration, number> = {
  half_day: 4 * 60,
  full_day: 8 * 60,
}

const SIGNIFICANT_GAP_RATIO = 0.25

export function classifyDurationFit(requestedMinutes: number, plannedMinutes: number): DurationFitStatus {
  if (!Number.isFinite(requestedMinutes) || requestedMinutes <= 0
    || !Number.isFinite(plannedMinutes) || plannedMinutes <= 0) {
    return 'fits'
  }
  if (plannedMinutes > requestedMinutes)
    return 'overflow'
  if (requestedMinutes - plannedMinutes >= requestedMinutes * SIGNIFICANT_GAP_RATIO)
    return 'underfilled'
  return 'fits'
}

export function hasSignificantDurationGap(duration: Duration, plannedMinutes: number): boolean {
  if (!Number.isFinite(plannedMinutes) || plannedMinutes <= 0)
    return false

  const requestedMinutes = REQUESTED_DURATION_MINUTES[duration]
  return Math.abs(plannedMinutes - requestedMinutes) >= requestedMinutes * SIGNIFICANT_GAP_RATIO
}
