import type { Item, StateResponse } from './collector-types'
import type { BotsPanelData } from './panel-data'

export interface SidebarBadge {
  label: string
  hot?: boolean
}

/** Live sidebar badges derived from collector state — replaces static mockup counts. */
export function sidebarBadgesFromState(state: StateResponse | null, items: Item[] = []): Record<string, SidebarBadge | undefined> {
  if (!state) return {}

  const bots = (state.panels.find((panel) => panel.id === 'bots')?.data as BotsPanelData | undefined)?.bots ?? []
  const downBots = bots.filter((bot) => bot.status === 'down').length
  const slowBots = bots.filter((bot) => bot.status === 'slow').length

  const inboxCount = items.length
  const decisionCount = items.filter((item) => item.kind === 'decision').length
  const agentsLive = (state.panels.find((panel) => panel.id === 'agents')?.data as { totalLive?: number } | undefined)?.totalLive

  return {
    '/inbox': inboxCount > 0 ? { label: String(inboxCount), hot: true } : undefined,
    '/decisions': decisionCount > 0 ? { label: String(decisionCount), hot: true } : undefined,
    '/bots': bots.length > 0
      ? { label: String(bots.length), hot: downBots > 0 || slowBots > 0 }
      : undefined,
    '/agents': agentsLive && agentsLive > 0 ? { label: String(agentsLive) } : undefined,
  }
}
