import type { FeedEvent } from '@cameleer/design-system';
import type { LogEntry } from '@cameleer/design-system';
import { UserPlus, UserMinus, RefreshCw, Play, Square, Clock, Skull, HeartPulse, Route, Send, Activity } from 'lucide-react';
import { createElement } from 'react';

export function formatUptime(seconds?: number): string {
  if (!seconds) return '\u2014';
  const days = Math.floor(seconds / 86400);
  const hours = Math.floor((seconds % 86400) / 3600);
  const mins = Math.floor((seconds % 3600) / 60);
  if (days > 0) return `${days}d ${hours}h`;
  if (hours > 0) return `${hours}h ${mins}m`;
  return `${mins}m`;
}

export function mapLogLevel(level: string): LogEntry['level'] {
  switch (level?.toUpperCase()) {
    case 'ERROR': return 'error';
    case 'WARN': case 'WARNING': return 'warn';
    case 'DEBUG': return 'debug';
    case 'TRACE': return 'trace';
    default: return 'info';
  }
}

export function eventSeverity(type: string): FeedEvent['severity'] {
  switch (type) {
    case 'WENT_DEAD':
    case 'AGENT_STOPPED':
    case 'DEREGISTERED': return 'error';
    case 'WENT_STALE': return 'warning';
    case 'RECOVERED':
    case 'REGISTERED':
    case 'RE_REGISTERED':
    case 'AGENT_STARTED': return 'success';
    default: return 'running';
  }
}

export function eventIcon(type: string) {
  switch (type) {
    case 'REGISTERED': return createElement(UserPlus, { size: 14 });
    case 'RE_REGISTERED': return createElement(RefreshCw, { size: 14 });
    case 'DEREGISTERED': return createElement(UserMinus, { size: 14 });
    case 'AGENT_STARTED': return createElement(Play, { size: 14 });
    case 'AGENT_STOPPED': return createElement(Square, { size: 14 });
    case 'WENT_STALE': return createElement(Clock, { size: 14 });
    case 'WENT_DEAD': return createElement(Skull, { size: 14 });
    case 'RECOVERED': return createElement(HeartPulse, { size: 14 });
    case 'ROUTE_STATE_CHANGED': return createElement(Route, { size: 14 });
    case 'COMMAND_DELIVERED':
    case 'COMMAND_ACKNOWLEDGED': return createElement(Send, { size: 14 });
    default: return createElement(Activity, { size: 14 });
  }
}
