import { Tag } from 'antd'

export function RenderPhaseTag(phase: string | undefined) {
  if (!phase) {
    return '-'
  }

  const phaseColorMap: Record<string, string> = {
    REPLAY_TASK_PHASE_UNSPECIFIED: 'default',
    REPLAY_TASK_PHASE_PENDING: 'processing',
    REPLAY_TASK_PHASE_RUNNING: 'success',
    REPLAY_TASK_PHASE_STOPPED: 'warning',
    REPLAY_TASK_PHASE_DELETED: 'error'
  }

  const color = phaseColorMap[phase] ?? 'default'
  const segments = phase.split('_')
  const last = segments[segments.length - 1]

  return <Tag color={color}>{last}</Tag>
}

export function RenderStateTag(
  state: string | undefined,
  type: 'current' | 'desired'
) {
  if (!state) {
    return '-'
  }

  const currentStateColorMap: Record<string, string> = {
    REPLAY_CURRENT_STATE_UNSPECIFIED: 'default',
    REPLAY_CURRENT_STATE_PENDING: 'default',
    REPLAY_CURRENT_STATE_DEPLOYING: 'processing',
    REPLAY_CURRENT_STATE_DEPLOYED: 'success',
    REPLAY_CURRENT_STATE_STARTING: 'processing',
    REPLAY_CURRENT_STATE_RUNNING: 'success',
    REPLAY_CURRENT_STATE_PAUSING: 'warning',
    REPLAY_CURRENT_STATE_PAUSED: 'warning',
    REPLAY_CURRENT_STATE_DELETING: 'error',
    REPLAY_CURRENT_STATE_DELETED: 'default'
  }

  const desiredStateColorMap: Record<string, string> = {
    REPLAY_DESIRED_STATE_UNSPECIFIED: 'default',
    REPLAY_DESIRED_STATE_DEPLOY: 'processing',
    REPLAY_DESIRED_STATE_ACTIVE: 'success',
    REPLAY_DESIRED_STATE_PAUSE: 'warning'
  }

  const colorMap =
    type === 'current' ? currentStateColorMap : desiredStateColorMap
  const color = colorMap[state] ?? 'default'

  const segments = state.split('_')
  const last = segments[segments.length - 1]

  return <Tag color={color}>{last}</Tag>
}
