// canned reminder moments offered in the add-reminder sheet: offsets from
// the visit when one is set, plus relative fallbacks — all at 9am local,
// filtered to the future and deduped when offsets coincide

const DAY_MS = 24 * 60 * 60 * 1000

const morningOf = (date: Date): Date => {
  const d = new Date(date)
  d.setHours(9, 0, 0, 0)
  return d
}

const daysFrom = (date: Date, days: number): Date =>
  new Date(date.getTime() + days * DAY_MS)

export type ReminderOption = {label: string; remindAt: Date}

export const reminderOptions = (
  anchor: Date | null,
  now: Date,
): ReminderOption[] => {
  const anchored: ReminderOption[] = anchor
    ? [
        {
          label: 'A week before',
          remindAt: morningOf(daysFrom(anchor, -7)),
        },
        {
          label: 'The day before',
          remindAt: morningOf(daysFrom(anchor, -1)),
        },
        {label: 'That morning', remindAt: morningOf(anchor)},
      ]
    : []
  const relative: ReminderOption[] = [
    {
      label: 'Tomorrow morning',
      remindAt: morningOf(daysFrom(now, 1)),
    },
    {label: 'Next week', remindAt: morningOf(daysFrom(now, 7))},
  ]
  const seen = new Set<number>()
  return [...anchored, ...relative].filter(({remindAt}) => {
    if (remindAt.getTime() <= now.getTime()) return false
    if (seen.has(remindAt.getTime())) return false
    seen.add(remindAt.getTime())
    return true
  })
}
