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

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

const STATUS_LABEL: Record<TenantStatus, string> = {
  active: 'Active',
  suspended: 'Suspended',
  archived: 'Archived',
};

const STATUS_VARIANT: Record<TenantStatus, 'success' | 'warning' | 'muted'> = {
  active: 'success',
  suspended: 'warning',
  archived: 'muted',
};

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

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

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

export function formatRateLimit(n: number): string {
  if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M/s`;
  if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k/s`;
  return `${n}/s`;
}

export function formatDate(iso: string | null | undefined): string {
  if (!iso) return '\u2014';
  const d = new Date(iso);
  // toLocaleDateString returns 'Invalid Date' rather than throwing
  // for unparseable input, so check the numeric time instead.
  if (Number.isNaN(d.getTime())) return iso;
  return d.toLocaleDateString(undefined, {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
  });
}
