import i18n from '../i18n/config'

export type CardStatus = 'todo' | 'doing' | 'done'

export interface Card {
  cardId: string
  title: string
  status: CardStatus | string
  position: number
  createdAt: string
}

export const STATUSES: CardStatus[] = ['todo', 'doing', 'done']

export function sortCards(cards: Card[]) {
  return [...cards].sort((a, b) => {
    if (a.status !== b.status) return a.status.localeCompare(b.status)
    return a.position - b.position
  })
}

export function classifyHealth(cards: Card[]) {
  const todo = cards.filter((card) => card.status === 'todo').length
  const doing = cards.filter((card) => card.status === 'doing').length
  if (doing === 0) return 'idle'
  if (todo > doing * 2) return 'backlogged'
  return 'flowing'
}

export function prettyStatus(status: string) {
  switch (status) {
    case 'todo':
      return i18n.t('status.todo')
    case 'doing':
      return i18n.t('status.doing')
    case 'done':
      return i18n.t('status.done')
    default:
      return status
  }
}

export function relativeTime(timestamp: string) {
  const delta = Math.max(0, Math.floor((Date.now() - new Date(timestamp).getTime()) / 1000))
  if (delta < 60) return i18n.t('relativeTime.seconds', { count: delta })
  if (delta < 3600) return i18n.t('relativeTime.minutes', { count: Math.floor(delta / 60) })
  if (delta < 86400) return i18n.t('relativeTime.hours', { count: Math.floor(delta / 3600) })
  return i18n.t('relativeTime.days', { count: Math.floor(delta / 86400) })
}
