import type { UserProgress } from '../api/flashcard-api'

export const EASE = {
  ignored: -1,
  hard: 0.1,
  easy: 0.65,
  known: 1.0,
} as const

export type EaseKey = keyof typeof EASE

const INTERVAL_LADDER: Record<Exclude<EaseKey, 'ignored'>, number[]> = {
  // count:  0  1  2  3   4   5+
  known:    [1, 3, 7, 14, 30, 60],
  easy:     [1, 2, 4, 8,  14, 30],
  hard:     [1, 1, 1, 2,  3,  5],
}

export function computeNextReview(key: EaseKey, reviewCount: number): string | null {
  if (key === 'ignored') return null
  const ladder = INTERVAL_LADDER[key]
  const days = ladder[Math.min(reviewCount, ladder.length - 1)]
  return new Date(Date.now() + days * 24 * 60 * 60 * 1000).toISOString()
}

export function statusFor(key: EaseKey): UserProgress['status'] {
  if (key === 'known') return 'known'
  if (key === 'ignored') return 'ignored'
  return 'learning'
}
