// src/components/keywords/row-actions.tsx — hover-revealed per-row actions.
"use client";

import {
  ArrowUpRightIcon,
  CopyIcon,
  RefreshCwIcon,
  SparklesIcon,
  Trash2Icon,
} from "lucide-react";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";

/** Hover-revealed row actions (design ELEVA-79): copy the term, open the
 *  keyword sheet, delete (with confirmation). Revealed on row hover with a
 *  slight slide-in; always visible to keyboard focus. */
export function KeywordRowActions({
  term,
  onDelete,
  onSync,
  onRelated,
}: {
  term: string;
  onDelete?: () => void;
  /** Enrichir CE mot-clé via Mangools (bouton masqué si absent — connecteur
   *  non branché ou source non disponible). */
  onSync?: () => void;
  /** Ouvrir le dialog « Mots-clés associés » (KWFinder related) pour CE mot-clé
   *  (bouton masqué si absent — connecteur Mangools non branché). */
  onRelated?: () => void;
}) {
  return (
    <span className="ml-auto flex translate-x-1 items-center gap-0.5 pl-2 opacity-0 transition-all duration-150 focus-within:translate-x-0 focus-within:opacity-100 group-hover:translate-x-0 group-hover:opacity-100">
      <Button
        variant="ghost"
        size="icon"
        className="size-6.5 rounded-[7px] text-muted-foreground hover:text-foreground"
        aria-label={`Copier ${term}`}
        title="Copier"
        onClick={(e) => {
          e.stopPropagation();
          void navigator.clipboard.writeText(term);
        }}
      >
        <CopyIcon className="size-3.5" />
      </Button>
      {/* "Ouvrir" has no onClick by design: the open handler is the table's
          `onRowClick` (wired on <TableRow> in data-table.tsx), not plumbed down
          to this cell. The button deliberately does NOT stopPropagation, so the
          click bubbles to the row and opens the keyword sheet. (ELEVA-86: keep
          bubbling rather than invent a handler the cell doesn't receive.) */}
      <Button
        variant="ghost"
        size="icon"
        className="size-6.5 rounded-[7px] text-muted-foreground hover:text-foreground"
        aria-label={`Ouvrir ${term}`}
        title="Ouvrir"
      >
        <ArrowUpRightIcon className="size-3.5" />
      </Button>
      {onRelated ? (
        <Button
          variant="ghost"
          size="icon"
          className="size-6.5 rounded-[7px] text-muted-foreground hover:text-foreground"
          aria-label={`Mots-clés associés à ${term}`}
          title="Mots-clés associés"
          onClick={(e) => {
            e.stopPropagation();
            onRelated();
          }}
        >
          <SparklesIcon className="size-3.5" />
        </Button>
      ) : null}
      {onSync ? (
        <Button
          variant="ghost"
          size="icon"
          className="size-6.5 rounded-[7px] text-muted-foreground hover:text-foreground"
          aria-label={`Enrichir ${term} via Mangools`}
          title="Enrichir via Mangools"
          onClick={(e) => {
            e.stopPropagation();
            onSync();
          }}
        >
          <RefreshCwIcon className="size-3.5" />
        </Button>
      ) : null}
      {onDelete ? (
        <AlertDialog>
          <AlertDialogTrigger asChild>
            <Button
              variant="ghost"
              size="icon"
              className="size-6.5 rounded-[7px] text-muted-foreground hover:bg-destructive/10 hover:text-destructive"
              aria-label={`Supprimer ${term}`}
              title="Supprimer"
              onClick={(e) => e.stopPropagation()}
            >
              <Trash2Icon className="size-3.5" />
            </Button>
          </AlertDialogTrigger>
          <AlertDialogContent onClick={(e) => e.stopPropagation()}>
            <AlertDialogHeader>
              <AlertDialogTitle>Supprimer « {term} » ?</AlertDialogTitle>
              <AlertDialogDescription>
                Cette action est irréversible.
              </AlertDialogDescription>
            </AlertDialogHeader>
            <AlertDialogFooter>
              <AlertDialogCancel>Annuler</AlertDialogCancel>
              <AlertDialogAction onClick={onDelete}>
                Supprimer
              </AlertDialogAction>
            </AlertDialogFooter>
          </AlertDialogContent>
        </AlertDialog>
      ) : null}
    </span>
  );
}
