import { Check, AlertTriangle, Send, Clock, X } from "lucide-react";
import { cn } from "@/lib/utils";
import type { InvoiceStatus } from "@rubis/shared";

/**
 * StatusBadge — pastille de statut de facture.
 *
 * Mapping marque-respectueux (cf. /docs/marque.md §3, "le rubis est rare") :
 *  - pending           : cream-2 + ink-2 — calme, attente
 *  - in_relance        : ink + cream      — actif, sombre
 *  - awaiting_user_confirmation : rubis solide + white — "à vous de jouer"
 *  - paid              : cream + ink + ✓  — accomplissement calme (pas de vert)
 *  - litigation        : rubis-deep + white — sérieux
 *  - cancelled         : line + ink-3      — muted
 */
type StatusVisual = {
  label: string;
  icon?: React.ComponentType<{ size?: number; "aria-hidden"?: boolean | "true" | "false" }>;
  className: string;
};

const STATUS_MAP: Record<InvoiceStatus, StatusVisual> = {
  pending: {
    label: "À relancer",
    icon: Clock,
    className: "bg-cream-2 text-ink-2 border-line",
  },
  in_relance: {
    label: "En relance",
    icon: Send,
    className: "bg-ink text-cream border-ink",
  },
  awaiting_user_confirmation: {
    label: "À valider",
    icon: AlertTriangle,
    className: "bg-rubis text-white border-rubis",
  },
  paid: {
    label: "Encaissée",
    icon: Check,
    className: "bg-white text-ink border-line",
  },
  litigation: {
    label: "Litige",
    icon: AlertTriangle,
    className: "bg-rubis-deep text-white border-rubis-deep",
  },
  cancelled: {
    label: "Annulée",
    icon: X,
    className: "bg-cream-2 text-ink-3 border-line",
  },
};

type StatusBadgeProps = {
  status: InvoiceStatus;
  /** Override le label (ex: "Relance J+3 envoyée"). */
  label?: string;
  /** Si true, masque l'icône — utile dans une cellule dense. */
  withoutIcon?: boolean;
  className?: string;
};

export function StatusBadge({ status, label, withoutIcon = false, className }: StatusBadgeProps) {
  const visual = STATUS_MAP[status];
  const Icon = visual.icon;
  return (
    <span
      className={cn(
        "inline-flex items-center gap-1.5 rounded-full border px-2.5 py-1",
        "font-sans text-[12px] font-medium leading-tight whitespace-nowrap",
        visual.className,
        className,
      )}
    >
      {!withoutIcon && Icon && <Icon size={12} aria-hidden="true" />}
      {label ?? visual.label}
    </span>
  );
}
