import type { AdminSeasonDetailResponse } from '../../types/awards'

export type AdminWorkflowPhaseKey = 'nomination' | 'voting' | 'preparation' | 'show' | 'completed'

export interface AdminReadinessScope {
  phaseKey: AdminWorkflowPhaseKey
  winnersPublished: boolean
  candidateWarningsActive: boolean
  reviewWarningsActive: boolean
  voteWarningsActive: boolean
  winnerWarningsActive: boolean
  publicationWarningsActive: boolean
}

export function normalizeAdminWorkflowPhase(currentPhase: string): AdminWorkflowPhaseKey {
  const value = currentPhase.trim().toLowerCase()
  if (value.includes('abgeschlossen') || value.includes('archiv') || value.includes('complete') || value.includes('ended')) {
    return 'completed'
  }
  if (value.includes('show')) return 'show'
  if (value.includes('aufbereit') || value.includes('vorbereit') || value.includes('pause') || value.includes('review') || value.includes('auswert')) {
    return 'preparation'
  }
  if (value.includes('vot')) return 'voting'
  return 'nomination'
}

export function buildAdminReadinessScope(season: Pick<AdminSeasonDetailResponse, 'currentPhase' | 'winnersPublishedAt'>): AdminReadinessScope {
  const phaseKey = normalizeAdminWorkflowPhase(season.currentPhase)
  const winnersPublished = Boolean(season.winnersPublishedAt)
  const afterNomination = phaseKey !== 'nomination'
  const afterVotingStarted = phaseKey === 'preparation' || phaseKey === 'show' || phaseKey === 'completed'
  const winnerWarningsActive = afterVotingStarted || winnersPublished

  return {
    phaseKey,
    winnersPublished,
    candidateWarningsActive: afterNomination,
    reviewWarningsActive: afterNomination,
    voteWarningsActive: afterVotingStarted,
    winnerWarningsActive,
    publicationWarningsActive: phaseKey === 'show' || phaseKey === 'completed' || winnersPublished,
  }
}

export function phaseLabel(phaseKey: AdminWorkflowPhaseKey) {
  if (phaseKey === 'nomination') return 'Nominierung'
  if (phaseKey === 'voting') return 'Voting'
  if (phaseKey === 'preparation') return 'Auswertung'
  if (phaseKey === 'show') return 'Award-Show'
  return 'Abgeschlossen'
}
