
import React from 'react';
import {
  AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent,
  AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Loader2, AlertTriangle } from 'lucide-react';
import { useRemoveProxyText } from '@/hooks/useRemoveProxyText';
import { useToast } from '@/hooks/use-toast';

export default function RemoveProxyTextDialog({ open, onOpenChange, clientId, onSuccess }) {
  const { removeProxyText, isRemoving } = useRemoveProxyText();
  const { toast } = useToast();

  const handleConfirm = async () => {
    try {
      const count = await removeProxyText(clientId);
      if (count > 0) {
        toast({ title: "Success", description: `Removed proxy information from ${count} owner records.` });
        onSuccess?.();
      } else {
        toast({ title: "No Changes", description: "No proxy information found to remove." });
      }
      onOpenChange(false);
    } catch (error) {
      // Error handling is done in the hook
    }
  };

  return (
    <AlertDialog open={open} onOpenChange={onOpenChange}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle className="flex items-center gap-2 text-destructive">
            <AlertTriangle className="h-5 w-5" />
            Remove All Proxy Info?
          </AlertDialogTitle>
          <AlertDialogDescription className="space-y-3">
            <p>This action will search all owners in this association and remove any text related to proxies (e.g., "(Proxy)", "Proxy Received").</p>
            <p className="font-medium text-foreground">This will also clear the "Proxy Received" indicator from the sign-in sheet.</p>
            <p className="text-destructive font-semibold">This action cannot be undone.</p>
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel disabled={isRemoving}>Cancel</AlertDialogCancel>
          <AlertDialogAction
            onClick={(e) => { e.preventDefault(); handleConfirm(); }}
            disabled={isRemoving}
            className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
          >
            {isRemoving ? (<><Loader2 className="mr-2 h-4 w-4 animate-spin" />Processing...</>) : "Yes, Remove Proxy Info"}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}
