// @design-system: domain/StatusBadge
import { Pill } from '@/components/ui/primitives/Pill';
import { useT, useLocale } from '@/lib/i18n/react';
import { enumLabel } from '@/lib/enums/enum-labels';

/** Pill tone enum mirrored from the Pill primitive. */
export type StatusTone = 'success' | 'warning' | 'danger' | 'info' | 'neutral';

/** One entry per status value: which tone to render and which i18n key to label. */
export interface StatusEntry {
  tone: StatusTone;
  labelKey: string;
}

type StatusAudience = 'admin' | 'vendor' | 'customer' | 'affiliate';

export interface StatusBadgeProps<S extends string> {
  state: S;
  map: Record<S, StatusEntry>;
  ns: string;
  size?: 'sm' | 'md';
  /** When set, label comes from enumLabel instead of the i18n namespace. */
  enumName?: string;
  audience?: StatusAudience;
}

/**
 * StatusBadge - generic state→Pill renderer. Domain wrappers (group-deal, return)
 * collapse to a `map` config object passed here instead of bespoke components.
 */
export function StatusBadge<S extends string>({
  state,
  map,
  ns,
  size = 'sm',
  enumName,
  audience = 'admin',
}: StatusBadgeProps<S>) {
  const t = useT(ns as Parameters<typeof useT>[0]);
  const { locale } = useLocale();
  const entry = map[state] ?? { tone: 'neutral' as StatusTone, labelKey: state };
  const label = enumName
    ? enumLabel(enumName, state, locale, audience)
    : t(entry.labelKey as never);
  return (
    <Pill tone={entry.tone} size={size}>
      {label}
    </Pill>
  );
}
