import {
  AlertDialog,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@/shared/common/ui/alert-dialog";
import { Button } from "@/shared/common/ui/button";
import { useErrorHandler } from "@/shared/hooks/useErrorHandler";
import { useEmployeePositions } from "@/user-management/hooks/useEmployeePostions";
import { Loader2 } from "lucide-react";
import { useState } from "react";
import { useTranslation } from "react-i18next";

interface ActivateArchivedUserProps {
    isOpen: boolean;
    onClose: () => void;
    userId: string;
}


const ActivateArchivedUser: React.FC<ActivateArchivedUserProps> = ({
    isOpen,
    onClose,
    userId,
}) => {
    const { activateUser, isActivatingUser } = useEmployeePositions();
    const [isActivatedUser, setIsActivatedUser] = useState(false);
    const {t} = useTranslation()
    const {handleError } = useErrorHandler(t)
    const onActviate = async () => {
        try {
            await activateUser({
                payload: userId,
                successCallback: () => {
                    onClose();
                },
            });
            setIsActivatedUser(true);
        }
        catch (error) {
            console.error("Error activating user:", error);
            handleError(error);
        }
    }
  return (
    <AlertDialog open={isOpen} onOpenChange={onClose}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>
            Activate Archived User?
          </AlertDialogTitle>
          <AlertDialogDescription>
           Are you sure you want to activate this archived user? This action will restore the user's access and data within the organization.
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel disabled={isActivatedUser}>
            Cancel
          </AlertDialogCancel>
          <Button
            variant="default"
            onClick={onActviate}
            disabled={isActivatedUser}>
            {isActivatingUser && (
              <Loader2 className="h-4 w-4 mr-2 animate-spin" />
            )}
            Activate
          </Button>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
};

export default ActivateArchivedUser;
