/**
 * Display formatters for the Telegram bots feature. Kept as
 * pure functions / components so they're easy to test
 * independently of React.
 */

import { Badge } from '@/components/ui/badge';
import type { TelegramBotStatus } from './types';

const STATUS_LABEL: Record<TelegramBotStatus, string> = {
  active: 'Active',
  paused: 'Paused',
};

const STATUS_VARIANT: Record<TelegramBotStatus, 'success' | 'warning'> = {
  active: 'success',
  paused: 'warning',
};

export function statusLabel(s: TelegramBotStatus): string {
  return STATUS_LABEL[s] ?? s;
}

export function statusVariant(s: TelegramBotStatus): 'success' | 'warning' {
  return STATUS_VARIANT[s] ?? 'warning';
}

export function StatusBadge({ status }: { status: TelegramBotStatus }) {
  return <Badge variant={statusVariant(status)}>{statusLabel(status)}</Badge>;
}

export function tokenSetLabel(set: boolean): string {
  return set ? 'Configured' : 'Not set';
}

export function tokenSetVariant(
  set: boolean,
): 'success' | 'warning' {
  return set ? 'success' : 'warning';
}

export function TokenSetBadge({ set }: { set: boolean }) {
  return <Badge variant={tokenSetVariant(set)}>{tokenSetLabel(set)}</Badge>;
}

export function formatDate(iso: string | null | undefined): string {
  if (!iso) return '\u2014';
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return iso;
  return d.toLocaleDateString(undefined, {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
  });
}

export function formatDateTime(iso: string | null | undefined): string {
  if (!iso) return '\u2014';
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return iso;
  return d.toLocaleString(undefined, {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
  });
}

/**
 * Truncate a welcome message / description for table display.
 * Keeps the first N chars and adds an ellipsis.
 */
export function truncate(s: string | undefined | null, n = 60): string {
  if (!s) return '\u2014';
  if (s.length <= n) return s;
  return s.slice(0, n - 1) + '\u2026';
}
