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

import { observer } from "mobx-react";
import { useTranslation } from "@plane/i18n";
import type { TInboxIssueCurrentTab } from "@plane/types";
import { EInboxIssueCurrentTab } from "@plane/types";
import { cn } from "@plane/utils";
import { useProjectExternalContoursBoard } from "@/hooks/store/use-project-external-contours-board";
import { useAppRouter } from "@/hooks/use-app-router";
import { ExternalContoursBoardFiltersRow } from "./board-filters-row";
import { ExternalContoursBoardColumn } from "./board-column";

type Props = {
  projectId: string;
  workspaceSlug: string;
};

const tabNavigationOptions: { key: TInboxIssueCurrentTab; i18nLabel: string }[] = [
  { key: EInboxIssueCurrentTab.OPEN, i18nLabel: "external_contours_page.tabs.open" },
  { key: EInboxIssueCurrentTab.CLOSED, i18nLabel: "external_contours_page.tabs.closed" },
];

export const ExternalContoursBoardRoot = observer(function ExternalContoursBoardRoot(props: Props) {
  const { projectId, workspaceSlug } = props;
  const { t } = useTranslation();
  const router = useAppRouter();
  const { currentTab, hasAnyItems, isFiltering, loader, tabCountMap, handleCurrentTab } = useProjectExternalContoursBoard();

  return (
    <div className="flex h-full min-h-0 flex-col overflow-hidden px-8 pb-6">
      <div className="flex shrink-0 items-center gap-2 py-4">
        <div className="nodedc-filter-row-shell flex items-center gap-2 p-1">
          {tabNavigationOptions.map((option) => {
            const count = tabCountMap[option.key] ?? 0;
            return (
              <button
                type="button"
                key={option.key}
                data-active={currentTab === option.key}
                className={cn("nodedc-external-tab flex min-w-[10rem] items-center justify-center gap-2 text-13 font-medium transition-all")}
                onClick={() => {
                  if (currentTab === option.key) return;
                  void handleCurrentTab(workspaceSlug, projectId, option.key);
                  router.push(`/${workspaceSlug}/projects/${projectId}/external-contours?currentTab=${option.key}`);
                }}
              >
                <div>{t(option.i18nLabel)}</div>
                <div
                  className={cn(
                    "rounded-full px-1.5 py-0.5 text-11 font-semibold",
                    currentTab === option.key ? "bg-accent-primary/15 text-accent-primary" : "bg-white/5 text-secondary"
                  )}
                >
                  {count}
                </div>
              </button>
            );
          })}
        </div>
      </div>

      <div className="shrink-0 pb-4">
        <ExternalContoursBoardFiltersRow workspaceSlug={workspaceSlug} projectId={projectId} />
      </div>

      {loader === "init-loading" && !hasAnyItems ? (
        <div className="flex flex-1 items-center justify-center text-13 text-secondary">{t("loading")}...</div>
      ) : (
        <div className="relative min-h-0 flex-1">
          {isFiltering && (
            <div className="pointer-events-none absolute top-0 right-0 z-10 rounded-full bg-black/35 px-3 py-1 text-11 font-medium text-secondary backdrop-blur-sm">
              {t("updating")}...
            </div>
          )}

          <div
            className={cn(
              "horizontal-scrollbar flex h-full min-h-0 items-stretch gap-4 overflow-x-auto overflow-y-hidden pb-1 transition-opacity",
              { "opacity-60": isFiltering }
            )}
          >
            <ExternalContoursBoardColumn
              currentTab={currentTab}
              direction="outgoing"
              projectId={projectId}
              workspaceSlug={workspaceSlug}
            />
            <ExternalContoursBoardColumn
              currentTab={currentTab}
              direction="incoming"
              projectId={projectId}
              workspaceSlug={workspaceSlug}
            />
          </div>
        </div>
      )}
    </div>
  );
});
