/**
 * Status pill with a deterministic status → tone mapping. The label is
 * always rendered as text (status never relies on color alone).
 */

type Tone = "green" | "amber" | "red" | "blue" | "slate";

const TONE_BY_STATUS: Record<string, Tone> = {
  ok: "green",
  valid: "green",
  ready: "green",
  complete: "green",
  fresh: "green",
  success: "green",
  linked: "green",
  connected: "green",
  running: "blue",
  building: "amber",
  warnings: "amber",
  warning: "amber",
  cached: "amber",
  partial: "amber",
  stale: "amber",
  error: "red",
  invalid: "red",
  failed: "red",
  rejected: "red",
  failed_no_cache: "red",
  missing: "red",
  skipped: "slate",
  unknown: "slate",
  pending: "slate",
};

export function toneForStatus(status: string | null | undefined): Tone {
  if (!status) return "slate";
  return TONE_BY_STATUS[status.toLowerCase()] ?? "slate";
}

export function StatusPill({
  status,
  label,
}: {
  status: string | null | undefined;
  label?: string;
}) {
  const text = label ?? status ?? "unknown";
  return (
    <span className={`pill tone-${toneForStatus(status)}`}>
      <span className="pill-dot" aria-hidden="true" />
      {text}
    </span>
  );
}
