"use client";

import { useMutation } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";

import { Button } from "@/components/ui/Button";
import { deleteVolunteerOpportunityRole } from "@/features/opportunities/services/roles";
import { useLanguageStore } from "@/store/languageStore";
import { getApiErrorMessages, getLocalizedApiMessage } from "@/lib/api/errors";

interface DeleteRoleModalProps {
  /** The role's integer id. */
  roleId: number | null;
  setOpenForm: () => void;
  refetch: () => void;
  dropdownRefetch?: () => void;
}

export default function DeleteVolunteerRoleModal({
  roleId,
  setOpenForm,
  refetch,
  dropdownRefetch,
}: DeleteRoleModalProps) {
  const { t } = useTranslation();
  const selectedLanguage = useLanguageStore((s) => s.language);
  const deleteRoleMutation = useMutation({
    mutationFn: deleteVolunteerOpportunityRole,
  });

  const handleDelete = async () => {
    if (!roleId) return;
    try {
      await deleteRoleMutation.mutateAsync(String(roleId));
      toast.success(t("COMMON.TOAST.DELETE_OPPURTUNITY_ROLE_SUCCESS"));
      setOpenForm();
      refetch();
      dropdownRefetch?.();
    } catch (err) {
      /* Both envelope shapes, and the localized pair, are handled by
         `lib/api/errors.ts`; this block used to re-implement the
         legacy `errors[field][lang]` walk by hand. */
      const messages = getApiErrorMessages(err, selectedLanguage);
      if (messages.length > 0) {
        messages.forEach((message) => toast.error(message));
      } else {
        toast.error(
          getLocalizedApiMessage(
            err,
            selectedLanguage,
            t("COMMON.TOAST.DELETE_OPPURTUNITY_ROLE_FAILED"),
          ),
        );
      }
    }
  };

  return (
    <div className="md:w-[100%] rounded-lg bg-white filtermodal">
      <h2 className="text-center text-lg pb-10">
        {t("COMMON.ARE_YOU_SURE_DELETE_ROLE")}
      </h2>
      <div className="flex xss:flex-col justify-center w-full gap-5">
        <Button
          variant="primary"
          type="submit"
          size="medium"
          onClick={handleDelete}
          disabled={deleteRoleMutation.isPending}
          className="xss:!w-full"
        >
          {t("COMMON.CONFIRM")}
        </Button>
        <Button
          variant="secondary"
          size="medium"
          type="button"
          onClick={() => setOpenForm()}
          disabled={deleteRoleMutation.isPending}
          className="xss:!w-full"
        >
          {t("COMMON.CANCEL")}
        </Button>
      </div>
    </div>
  );
}
