import type { ReactNode } from 'react';
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from '@/components/ui/alert-dialog';

// Confirmation for the destructive and the irreversible-ish. Upstream routes every one of these through a
// single dialog store with the same "are you sure" copy; this keeps the single gate but says what will
// actually happen, because "are you sure?" on its own never told anyone anything.

export type ConfirmState = {
  title: string;
  description: ReactNode;
  confirmLabel?: string;
  destructive?: boolean;
  onConfirm: () => void;
} | null;

export const ConfirmDialog = ({ state, onClose }: { state: ConfirmState; onClose: () => void }) => {
  if (!state) return null;

  return (
    <AlertDialog open onOpenChange={(open) => !open && onClose()}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>{state.title}</AlertDialogTitle>
          <AlertDialogDescription>{state.description}</AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction
            onClick={() => {
              state.onConfirm();
              onClose();
            }}
            className={state.destructive ? 'bg-red-600 text-white hover:bg-red-700' : ''}
          >
            {state.confirmLabel ?? 'Confirm'}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
};
