import { useAwardsStore } from '../stores/awards'

/** Teilnahme-Aktionen und die Phase (Timeline-key), an die sie gekoppelt sind. */
export type ActionKey = 'nominate' | 'clip' | 'vote'

export const PHASE_KEY: Record<ActionKey, string> = {
  nominate: 'nomination',
  clip: 'nomination', // Clips laufen im selben Fenster wie die Nominierung
  vote: 'voting',
}

export type PhaseState = 'upcoming' | 'active' | 'done'

export interface PhaseInfo {
  state: PhaseState
  open: boolean
  startLabel: string
  endLabel: string
}

function formatDate(date?: string): string {
  if (!date) return ''
  const parsed = new Date(`${date}T00:00:00`)
  return Number.isNaN(parsed.getTime())
    ? date
    : parsed.toLocaleDateString('de-DE', { day: '2-digit', month: 'long' })
}

export function usePhases() {
  const store = useAwardsStore()

  function infoForPhaseKey(key?: string): PhaseInfo {
    const item = store.overview.timeline.find((entry) => entry.key === key)
    const state = (item?.state ?? 'upcoming') as PhaseState
    return {
      state,
      open: state === 'active',
      startLabel: formatDate(item?.startsAt),
      endLabel: formatDate(item?.endsAt),
    }
  }

  function phaseInfo(action: ActionKey): PhaseInfo {
    return infoForPhaseKey(PHASE_KEY[action])
  }

  return { phaseInfo, infoForPhaseKey }
}
