/**
 * Copyright (c) 2023-present Plane Software, Inc. and contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 * See the LICENSE file for details.
 */

import Link from "next/link";
import { observer } from "mobx-react";
import { useParams, usePathname } from "next/navigation";
import { EUserPermissions, EUserPermissionsLevel } from "@plane/constants";
import { useTranslation } from "@plane/i18n";
import { InboxIcon, PlusIcon } from "@plane/propel/icons";
import { Tooltip } from "@plane/propel/tooltip";
import useSWR from "swr";
import { TopNavPowerK } from "@/components/navigation";
import { useCommandPalette } from "@/hooks/store/use-command-palette";
import { useProject } from "@/hooks/store/use-project";
import { useUserPermissions } from "@/hooks/store/user";
import { UserMenuRoot } from "@/components/workspace/sidebar/user-menu-root";
import { useWorkspaceNotifications } from "@/hooks/store/notifications";

export const SidebarUtilityRail = observer(function SidebarUtilityRail() {
  const { workspaceSlug } = useParams();
  const pathname = usePathname();
  const { t } = useTranslation();
  const { toggleCreateIssueModal } = useCommandPalette();
  const { joinedProjectIds } = useProject();
  const { allowPermissions } = useUserPermissions();
  const { unreadNotificationsCount, getUnreadNotificationsCount } = useWorkspaceNotifications();

  const canCreateIssue = allowPermissions(
    [EUserPermissions.ADMIN, EUserPermissions.MEMBER],
    EUserPermissionsLevel.WORKSPACE
  );
  const isCreateDisabled = joinedProjectIds.length === 0 || !canCreateIssue;
  const isExternalContoursRoute = pathname?.includes("/external-contours");

  useSWR(
    workspaceSlug ? "WORKSPACE_UNREAD_NOTIFICATION_COUNT" : null,
    workspaceSlug ? () => getUnreadNotificationsCount(workspaceSlug.toString()) : null
  );

  const isMentionsEnabled = unreadNotificationsCount.mention_unread_notifications_count > 0;
  const totalNotifications = isMentionsEnabled
    ? unreadNotificationsCount.mention_unread_notifications_count
    : unreadNotificationsCount.total_unread_notifications_count;

  return (
    <div className="flex items-end justify-between gap-3">
      <div className="flex flex-col items-start gap-2 pl-2.5">
        {!isExternalContoursRoute && (
          <Tooltip tooltipContent={t("app_header.add_task")} position="right">
            <button
              type="button"
              className="relative flex size-8 items-center justify-center rounded-full border-0 bg-white/[0.04] text-tertiary backdrop-blur-[18px] transition-all hover:bg-white/[0.07] hover:text-primary disabled:pointer-events-none disabled:opacity-40"
              onClick={() => toggleCreateIssueModal(true)}
              disabled={isCreateDisabled}
              aria-label={t("app_header.add_task")}
            >
              <PlusIcon className="size-4" />
            </button>
          </Tooltip>
        )}
        <TopNavPowerK variant="sidebar" />
        <Tooltip tooltipContent="Уведомления" position="right">
          <Link
            href={`/${workspaceSlug?.toString()}/notifications/`}
            className="relative flex size-8 items-center justify-center rounded-full border-0 bg-white/[0.04] text-tertiary backdrop-blur-[18px] transition-all hover:bg-white/[0.07] hover:text-primary"
          >
            <InboxIcon className="size-4" />
            {totalNotifications > 0 && (
              <span className="absolute top-1.5 right-1.5 size-2 rounded-full bg-danger-primary" />
            )}
            <span className="sr-only">Уведомления</span>
          </Link>
        </Tooltip>
        <Tooltip tooltipContent="Профиль" position="right">
          <div>
            <UserMenuRoot variant="sidebar-utility" />
          </div>
        </Tooltip>
      </div>
    </div>
  );
});
