"use client";

import { SendHorizonalIcon, ShareIcon, TrashIcon } from "lucide-react";
import { useRouter } from "next/navigation";
import { useMemo, useState } from "react";
import toast from "react-hot-toast";
import { useTranslation } from "react-i18next";
import { TMember } from "@formbricks/types/memberships";
import { TOrganization } from "@formbricks/types/organizations";
import { getFormattedErrorMessage } from "@/lib/utils/helper";
import {
  createInviteTokenAction,
  deleteInviteAction,
  deleteMembershipAction,
  resendInviteAction,
} from "@/modules/organization/settings/teams/actions";
import { ShareInviteModal } from "@/modules/organization/settings/teams/components/invite-member/share-invite-modal";
import { TInvite } from "@/modules/organization/settings/teams/types/invites";
import { Button } from "@/modules/ui/components/button";
import { DeleteDialog } from "@/modules/ui/components/delete-dialog";
import { TooltipRenderer } from "@/modules/ui/components/tooltip";

interface MemberActionsProps {
  organization: TOrganization;
  member?: TMember;
  invite?: TInvite;
  showDeleteButton?: boolean;
}

export const MemberActions = ({ organization, member, invite, showDeleteButton }: MemberActionsProps) => {
  const router = useRouter();
  const { t } = useTranslation();
  const [isDeleteMemberModalOpen, setDeleteMemberModalOpen] = useState(false);
  const [isDeleting, setIsDeleting] = useState(false);
  const [showShareInviteModal, setShowShareInviteModal] = useState(false);
  const [shareInviteToken, setShareInviteToken] = useState("");

  const handleDeleteMember = async () => {
    try {
      setIsDeleting(true);
      if (!member && invite) {
        // This is an invite

        const result = await deleteInviteAction({ inviteId: invite?.id });
        if (result?.serverError) {
          toast.error(getFormattedErrorMessage(result));
          setIsDeleting(false);
          return;
        }
        toast.success(t("environments.settings.general.invite_deleted_successfully"));
      }

      if (member && !invite) {
        // This is a member

        const result = await deleteMembershipAction({
          userId: member.userId,
          organizationId: organization.id,
        });
        if (result?.serverError) {
          toast.error(getFormattedErrorMessage(result));
          setIsDeleting(false);
          return;
        }
        toast.success(t("environments.settings.general.member_deleted_successfully"));
      }

      setIsDeleting(false);
      router.refresh();
    } catch (err) {
      setIsDeleting(false);
      toast.error(t("common.something_went_wrong_please_try_again"));
    }
  };

  const memberName = useMemo(() => {
    if (member) {
      return member.name;
    }

    if (invite) {
      return invite.name;
    }

    return "";
  }, [invite, member]);

  const handleShareInvite = async () => {
    try {
      if (!invite) return;
      const createInviteTokenResponse = await createInviteTokenAction({ inviteId: invite.id });
      if (createInviteTokenResponse?.data) {
        setShareInviteToken(createInviteTokenResponse.data.inviteToken);
        setShowShareInviteModal(true);
        router.refresh();
      } else {
        const errorMessage = getFormattedErrorMessage(createInviteTokenResponse);
        toast.error(errorMessage);
      }
    } catch (err) {
      toast.error(`${t("common.error")}: ${err instanceof Error ? err.message : "Unknown error occurred"}`);
    }
  };

  const handleResendInvite = async () => {
    try {
      if (!invite) return;

      const resendInviteResponse = await resendInviteAction({
        inviteId: invite.id,
        organizationId: organization.id,
      });
      if (resendInviteResponse?.data) {
        toast.success(t("environments.settings.general.invitation_sent_once_more"));
        router.refresh();
      } else {
        const errorMessage = getFormattedErrorMessage(resendInviteResponse);
        toast.error(errorMessage);
      }
    } catch (err) {
      toast.error(`${t("common.error")}: ${err instanceof Error ? err.message : "Unknown error occurred"}`);
    }
  };

  return (
    <div className="flex justify-end gap-2">
      <TooltipRenderer tooltipContent={t("common.delete")} shouldRender={!!showDeleteButton}>
        <Button
          variant="destructive"
          size="icon"
          id="deleteMemberButton"
          disabled={!showDeleteButton}
          onClick={() => setDeleteMemberModalOpen(true)}>
          <TrashIcon />
        </Button>
      </TooltipRenderer>

      <TooltipRenderer
        tooltipContent={t("environments.settings.general.share_invite_link")}
        shouldRender={!!invite}>
        <Button
          variant="secondary"
          size="icon"
          id="shareInviteButton"
          disabled={!invite}
          onClick={() => {
            handleShareInvite();
          }}>
          <ShareIcon />
        </Button>
      </TooltipRenderer>

      <TooltipRenderer
        tooltipContent={t("environments.settings.general.resend_invitation_email")}
        shouldRender={!!invite}>
        <Button
          variant="secondary"
          size="icon"
          id="resendInviteButton"
          disabled={!invite}
          onClick={() => {
            handleResendInvite();
          }}>
          <SendHorizonalIcon />
        </Button>
      </TooltipRenderer>

      <DeleteDialog
        open={isDeleteMemberModalOpen}
        setOpen={setDeleteMemberModalOpen}
        deleteWhat={t("environments.settings.general.from_your_organization", { memberName })}
        onDelete={handleDeleteMember}
        isDeleting={isDeleting}
        text={t("environments.settings.general.delete_member_confirmation")}
      />

      {showShareInviteModal && (
        <ShareInviteModal
          inviteToken={shareInviteToken}
          open={showShareInviteModal}
          setOpen={setShowShareInviteModal}
        />
      )}
    </div>
  );
};
