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

import type { ReactNode } from "react";
import { observer } from "mobx-react";
import { CheckCircle, Clock, MoreVertical } from "lucide-react";
import { useTranslation } from "@pi-dash/i18n";
// pi dash imports
import { ArchiveIcon, CheckIcon } from "@pi-dash/propel/icons";
import type { TNotificationFilter } from "@pi-dash/types";
import { PopoverMenu } from "@pi-dash/ui";
// hooks
import { useWorkspaceNotifications } from "@/hooks/store/notifications";
// local imports
import { NotificationMenuOptionItem } from "./menu-item";
import { IconButton } from "@pi-dash/propel/icon-button";

export type TPopoverMenuOptions = {
  key: string;
  type: string;
  label?: string | undefined;
  isActive?: boolean | undefined;
  prependIcon?: ReactNode | undefined;
  appendIcon?: ReactNode | undefined;
  onClick?: (() => void) | undefined;
};

export const NotificationHeaderMenuOption = observer(function NotificationHeaderMenuOption() {
  // hooks
  const { filters, updateFilters, updateBulkFilters } = useWorkspaceNotifications();
  const { t } = useTranslation();

  const handleFilterChange = (filterType: keyof TNotificationFilter, filterValue: boolean) =>
    updateFilters(filterType, filterValue);

  const handleBulkFilterChange = (filter: Partial<TNotificationFilter>) => updateBulkFilters(filter);

  const popoverMenuOptions: TPopoverMenuOptions[] = [
    {
      key: "menu-unread",
      type: "menu-item",
      label: t("Show unread"),
      isActive: filters?.read,
      prependIcon: <CheckCircle className="h-3 w-3 flex-shrink-0" />,
      appendIcon: filters?.read ? <CheckIcon className="h-3 w-3" /> : undefined,
      onClick: () => handleFilterChange("read", !filters?.read),
    },
    {
      key: "menu-archived",
      type: "menu-item",
      label: t("Show archived"),
      isActive: filters?.archived,
      prependIcon: <ArchiveIcon className="h-3 w-3 flex-shrink-0" />,
      appendIcon: filters?.archived ? <CheckIcon className="h-3 w-3" /> : undefined,
      onClick: () =>
        handleBulkFilterChange({
          archived: !filters?.archived,
          snoozed: false,
        }),
    },
    {
      key: "menu-snoozed",
      type: "menu-item",
      label: t("Show snoozed"),
      isActive: filters?.snoozed,
      prependIcon: <Clock className="h-3 w-3 flex-shrink-0" />,
      appendIcon: filters?.snoozed ? <CheckIcon className="h-3 w-3" /> : undefined,
      onClick: () =>
        handleBulkFilterChange({
          snoozed: !filters?.snoozed,
          archived: false,
        }),
    },
  ];

  return (
    <PopoverMenu
      data={popoverMenuOptions}
      button={<IconButton size="base" variant="ghost" icon={MoreVertical} />}
      keyExtractor={(item: TPopoverMenuOptions) => item.key}
      panelClassName="p-0 py-2 rounded-md border border-subtle bg-surface-1 space-y-1"
      render={(item: TPopoverMenuOptions) => <NotificationMenuOptionItem {...item} />}
    />
  );
});
