"use client";

import {
  BellIcon,
  BlocksIcon,
  BrushIcon,
  Building2Icon,
  ChartColumnIcon,
  ChevronDownIcon,
  CodeXmlIcon,
  CreditCardIcon,
  DatabaseIcon,
  FoldersIcon,
  GlobeIcon,
  HourglassIcon,
  KeyIcon,
  LanguagesIcon,
  ListChecksIcon,
  ShieldIcon,
  TagIcon,
  UnplugIcon,
  UserCircleIcon,
  UsersIcon,
} from "lucide-react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { useTranslation } from "react-i18next";
import { TOrganizationRole } from "@formbricks/types/memberships";
import { cn } from "@/lib/cn";
import { getAccessFlags } from "@/lib/membership/utils";
import {
  SWITCHER_DROPDOWN_CONTENT_CLASS_NAME,
  SwitcherDropdownBody,
} from "@/modules/settings/components/switcher-dropdown-body";
import {
  accountSettingsPath,
  organizationSettingsPath,
  workspaceSettingsPath,
} from "@/modules/settings/lib/routes";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuTrigger,
} from "@/modules/ui/components/dropdown-menu";
import { Popover, PopoverContent, PopoverTrigger } from "@/modules/ui/components/popover";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/modules/ui/components/tooltip";

interface SettingsSidebarContentProps {
  readonly workspaceId: string;
  readonly workspaceName: string;
  readonly organizationId: string;
  readonly organizationName: string;
  readonly membershipRole?: TOrganizationRole;
  readonly isFormbricksCloud: boolean;
  readonly isCollapsed: boolean;
  readonly isTextVisible: boolean;
  // Hidden when the user has no workspace (org/account settings still render).
  readonly hideWorkspaceSection?: boolean;
  // Workspace switcher
  readonly workspaces: { id: string; name: string }[];
  readonly isLoadingWorkspaces: boolean;
  readonly onWorkspaceChange: (id: string) => void;
  readonly onWorkspaceDropdownOpen: () => void;
  readonly errorWorkspaces?: string | null;
  readonly onWorkspaceRetry?: () => void;
  // Organization switcher
  readonly organizations: { id: string; name: string }[];
  readonly isLoadingOrganizations: boolean;
  readonly onOrganizationChange: (id: string) => void;
  readonly onOrganizationDropdownOpen: () => void;
  readonly errorOrganizations?: string | null;
  readonly onOrganizationRetry?: () => void;
}

interface NavItem {
  id: string;
  label: string;
  href: string;
  icon: React.ReactNode;
  hidden?: boolean;
  disabled?: boolean;
}

const SettingsNavLink = ({
  item,
  isActive,
  isCollapsed,
  isTextVisible,
  disabledMessage,
}: Readonly<{
  item: NavItem;
  isActive: boolean;
  isCollapsed: boolean;
  isTextVisible: boolean;
  disabledMessage?: string;
}>) => {
  const activeClass = "bg-slate-50 border-r-4 border-brand-dark font-semibold text-slate-900";
  const inactiveClass =
    "hover:bg-slate-50 border-r-4 border-transparent hover:border-slate-300 transition-all duration-150 ease-in-out";
  const disabledClass = "cursor-not-allowed border-r-4 border-transparent text-slate-400";

  const isDisabled = item.disabled;
  // Workspace requests update the active workspace cookie, so only fetch them on navigation.
  const prefetch = item.href.startsWith("/workspaces/") ? false : null;

  const getStateClass = () => {
    if (isDisabled) return disabledClass;
    return isActive ? activeClass : inactiveClass;
  };

  if (isCollapsed) {
    return (
      <TooltipProvider delayDuration={0}>
        <Tooltip>
          <TooltipTrigger asChild>
            <li className={cn("rounded-l-md py-1.5 pl-2 text-sm", getStateClass())}>
              {isDisabled ? (
                <div className="flex items-center">{item.icon}</div>
              ) : (
                <Link
                  href={item.href}
                  prefetch={prefetch}
                  className="flex items-center text-slate-600 hover:text-slate-900">
                  {item.icon}
                </Link>
              )}
            </li>
          </TooltipTrigger>
          <TooltipContent side="right">
            {isDisabled ? disabledMessage || item.label : item.label}
          </TooltipContent>
        </Tooltip>
      </TooltipProvider>
    );
  }

  if (isDisabled) {
    return (
      <li className={cn("rounded-l-md py-1.5 pl-8 text-sm", disabledClass)}>
        <Popover>
          <PopoverTrigger asChild>
            <div className="flex items-center">
              {item.icon}
              <span
                className={cn(
                  "ml-2 transition-opacity duration-100",
                  isTextVisible ? "opacity-0" : "opacity-100"
                )}>
                {item.label}
              </span>
            </div>
          </PopoverTrigger>
          <PopoverContent className="w-fit max-w-72 px-3 py-2 text-sm text-slate-700">
            {disabledMessage || item.label}
          </PopoverContent>
        </Popover>
      </li>
    );
  }

  return (
    <li
      className={cn(
        "rounded-l-md py-1.5 pl-8 text-sm",
        isActive ? activeClass : inactiveClass,
        "text-slate-600 hover:text-slate-900"
      )}>
      <Link href={item.href} prefetch={prefetch} className="flex items-center">
        {item.icon}
        <span
          className={cn("ml-2 transition-opacity duration-100", isTextVisible ? "opacity-0" : "opacity-100")}>
          {item.label}
        </span>
      </Link>
    </li>
  );
};

const SectionHeader = ({
  label,
  switcherType,
  isCollapsed,
  isTextVisible,
  switcherName,
  switcherItems,
  isLoadingSwitcher,
  errorSwitcher,
  onSwitcherRetry,
  currentId,
  onSwitcherChange,
  onSwitcherOpen,
}: Readonly<{
  label: string;
  switcherType?: "organization" | "workspace";
  isCollapsed: boolean;
  isTextVisible: boolean;
  switcherName?: string;
  switcherItems?: { id: string; name: string }[];
  isLoadingSwitcher?: boolean;
  errorSwitcher?: string | null;
  onSwitcherRetry?: () => void;
  currentId?: string;
  onSwitcherChange?: (id: string) => void;
  onSwitcherOpen?: () => void;
}>) => {
  if (isCollapsed) {
    return null;
  }

  return (
    <div
      className={cn(
        "mt-4 mb-1 flex min-w-0 items-center gap-2 px-4",
        isTextVisible ? "opacity-0" : "opacity-100"
      )}>
      <span className="shrink-0 text-xs font-semibold tracking-wider text-slate-500 uppercase">{label}</span>
      {switcherType && switcherName && switcherItems && onSwitcherChange && (
        <DropdownMenu onOpenChange={(open) => open && onSwitcherOpen?.()}>
          <DropdownMenuTrigger className="ml-auto flex max-w-[50%] min-w-0 items-center gap-1 rounded-md border border-slate-200 px-2 py-0.5 text-xs text-slate-600 hover:bg-slate-50">
            <span className="truncate">{switcherName}</span>
            <ChevronDownIcon className="size-3" />
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end" className={SWITCHER_DROPDOWN_CONTENT_CLASS_NAME}>
            <SwitcherDropdownBody
              type={switcherType}
              isLoading={isLoadingSwitcher ?? false}
              error={errorSwitcher ?? null}
              onRetry={() => onSwitcherRetry?.()}
              items={switcherItems}
              selectedId={currentId ?? ""}
              onSelect={onSwitcherChange}
              showSettings={false}
            />
          </DropdownMenuContent>
        </DropdownMenu>
      )}
    </div>
  );
};

export const SettingsSidebarContent = ({
  workspaceId,
  workspaceName,
  organizationId,
  organizationName,
  membershipRole,
  isFormbricksCloud,
  isCollapsed,
  isTextVisible,
  hideWorkspaceSection = false,
  workspaces,
  isLoadingWorkspaces,
  onWorkspaceChange,
  onWorkspaceDropdownOpen,
  errorWorkspaces,
  onWorkspaceRetry,
  organizations,
  isLoadingOrganizations,
  onOrganizationChange,
  onOrganizationDropdownOpen,
  errorOrganizations,
  onOrganizationRetry,
}: SettingsSidebarContentProps) => {
  const pathname = usePathname();
  const { t } = useTranslation();
  const { isMember, isBilling, isOwner, isManager, isDashboardViewer } = getAccessFlags(membershipRole);
  const isOwnerOrManager = isOwner || isManager;
  const iconClassName = "h-4 w-4 shrink-0";

  // Workspace items stay nested under the workspace; organization and account settings are now
  // scoped to their own top-level routes so they work with or without a current workspace. Paths
  // come from the shared route helpers so they can't drift from redirects/other navigation.
  const workspaceItems: NavItem[] = [
    {
      id: "general",
      label: t("common.general"),
      href: workspaceSettingsPath(workspaceId, "general"),
      icon: <FoldersIcon className={iconClassName} />,
      disabled: isBilling,
    },
    {
      id: "teams",
      label: t("common.team_access"),
      href: workspaceSettingsPath(workspaceId, "teams"),
      icon: <UsersIcon className={iconClassName} />,
      disabled: isBilling,
    },
    {
      id: "languages",
      label: t("common.survey_languages"),
      href: workspaceSettingsPath(workspaceId, "languages"),
      icon: <LanguagesIcon className={iconClassName} />,
      disabled: isBilling,
    },
    // Directly below Survey Languages: both are things a survey is composed from rather than things
    // the workspace is connected to, and an author reaches for them at the same point.
    {
      id: "embedded-data",
      label: t("common.embedded_data"),
      href: workspaceSettingsPath(workspaceId, "embedded-data"),
      icon: <DatabaseIcon className={iconClassName} />,
      disabled: isBilling,
    },
    {
      id: "app-connection",
      label: t("common.web_and_mobile_sdk"),
      href: workspaceSettingsPath(workspaceId, "app-connection"),
      icon: <CodeXmlIcon className={iconClassName} />,
      disabled: isBilling,
    },
    {
      id: "integrations",
      label: t("common.integrations"),
      href: workspaceSettingsPath(workspaceId, "integrations"),
      icon: <BlocksIcon className={iconClassName} />,
      disabled: isBilling,
    },
    {
      id: "look",
      label: t("common.appearance"),
      href: workspaceSettingsPath(workspaceId, "look"),
      icon: <BrushIcon className={iconClassName} />,
      disabled: isBilling,
    },
    {
      id: "user-actions",
      label: t("common.user_actions"),
      href: workspaceSettingsPath(workspaceId, "user-actions"),
      icon: <ListChecksIcon className={iconClassName} />,
      disabled: isBilling,
    },
    {
      id: "tags",
      label: t("common.tags"),
      href: workspaceSettingsPath(workspaceId, "tags"),
      icon: <TagIcon className={iconClassName} />,
      disabled: isBilling,
    },
  ];

  const organizationItems: NavItem[] = [
    {
      id: "org-general",
      label: t("common.general"),
      href: organizationSettingsPath(organizationId, "general"),
      icon: <Building2Icon className={iconClassName} />,
      disabled: isBilling,
    },
    {
      id: "org-teams",
      label: t("common.teams"),
      href: organizationSettingsPath(organizationId, "teams"),
      icon: <UsersIcon className={iconClassName} />,
      disabled: isBilling,
    },
    {
      id: "org-api-keys",
      label: t("common.api_keys"),
      href: organizationSettingsPath(organizationId, "api-keys"),
      icon: <KeyIcon className={iconClassName} />,
      hidden: !isOwnerOrManager,
    },
    {
      id: "org-feedback-directories",
      label: t("workspace.settings.feedback_directories.nav_label"),
      href: organizationSettingsPath(organizationId, "feedback-directories"),
      icon: <FoldersIcon className={iconClassName} />,
      hidden: isMember,
      disabled: !isOwnerOrManager,
    },
    {
      id: "org-data-retention",
      label: t("workspace.settings.data_retention.nav_label"),
      href: organizationSettingsPath(organizationId, "data-retention"),
      icon: <HourglassIcon className={iconClassName} />,
      // Members read the policies and exemptions; History is owners and managers only (ENG-3695).
      hidden: isBilling,
    },
    {
      id: "org-domain",
      label: t("common.domain"),
      href: organizationSettingsPath(organizationId, "domain"),
      icon: <GlobeIcon className={iconClassName} />,
      hidden: isFormbricksCloud,
    },
    {
      id: "org-billing",
      label: t("common.billing"),
      href: organizationSettingsPath(organizationId, "billing"),
      icon: <CreditCardIcon className={iconClassName} />,
      hidden: !isFormbricksCloud,
    },
    {
      id: "org-enterprise",
      label: t("common.enterprise_license"),
      href: organizationSettingsPath(organizationId, "enterprise"),
      icon: <ShieldIcon className={iconClassName} />,
      hidden: isFormbricksCloud,
      disabled: isMember || isBilling,
    },
    {
      id: "org-usage",
      label: t("workspace.settings.usage.nav_label"),
      href: organizationSettingsPath(organizationId, "usage"),
      icon: <ChartColumnIcon className={iconClassName} />,
      hidden: !isOwnerOrManager || isFormbricksCloud,
    },
  ];

  const accountItems: NavItem[] = [
    {
      id: "profile",
      label: t("common.your_profile"),
      href: accountSettingsPath("profile"),
      icon: <UserCircleIcon className={iconClassName} />,
    },
    {
      id: "notifications",
      label: t("common.notifications"),
      href: accountSettingsPath("notifications"),
      icon: <BellIcon className={iconClassName} />,
      disabled: isBilling,
    },
    {
      id: "authorized-apps",
      label: t("common.authorized_apps"),
      href: accountSettingsPath("authorized-apps"),
      icon: <UnplugIcon className={iconClassName} />,
      // Kept for a Dashboard viewer (U2): it can grant an app dashboard reads over OAuth, so it must be
      // able to see and revoke what it granted.
      disabled: isBilling,
    },
  ];

  // A Dashboard viewer (ADR-0025) keeps the organization switcher, Profile and Notifications, and none of
  // the workspace or organization settings.
  const visibleOrganizationItems = isDashboardViewer ? [] : organizationItems;
  const isWorkspaceSectionHidden = hideWorkspaceSection || isDashboardViewer;

  const disabledMessage = t("common.you_are_not_authorized_to_perform_this_action");

  const renderSection = (items: NavItem[]) => {
    const visibleItems = items.filter((item) => !item.hidden);
    return (
      <ul className="space-y-0.5">
        {visibleItems.map((item) => (
          <SettingsNavLink
            key={item.id}
            item={item}
            isActive={pathname.includes(item.href)}
            isCollapsed={isCollapsed}
            isTextVisible={isTextVisible}
            disabledMessage={item.disabled ? disabledMessage : undefined}
          />
        ))}
      </ul>
    );
  };

  return (
    <div className="flex flex-col overflow-y-auto">
      {!isWorkspaceSectionHidden && (
        <div>
          <SectionHeader
            label={t("common.workspace")}
            switcherType="workspace"
            isCollapsed={isCollapsed}
            isTextVisible={isTextVisible}
            switcherName={workspaceName}
            switcherItems={workspaces}
            isLoadingSwitcher={isLoadingWorkspaces}
            errorSwitcher={errorWorkspaces}
            onSwitcherRetry={onWorkspaceRetry}
            currentId={workspaceId}
            onSwitcherChange={onWorkspaceChange}
            onSwitcherOpen={onWorkspaceDropdownOpen}
          />
          {renderSection(workspaceItems)}
        </div>
      )}

      {/* A section with nothing to open (a Dashboard viewer's organization) is left out rather than shown
          as a bare heading; the main navigation still switches organizations. */}
      {visibleOrganizationItems.length > 0 && (
        <div>
          <SectionHeader
            label={t("common.organization")}
            switcherType="organization"
            isCollapsed={isCollapsed}
            isTextVisible={isTextVisible}
            switcherName={organizationName}
            switcherItems={organizations}
            isLoadingSwitcher={isLoadingOrganizations}
            errorSwitcher={errorOrganizations}
            onSwitcherRetry={onOrganizationRetry}
            currentId={organizationId}
            onSwitcherChange={onOrganizationChange}
            onSwitcherOpen={onOrganizationDropdownOpen}
          />
          {renderSection(visibleOrganizationItems)}
        </div>
      )}

      <div>
        <SectionHeader label={t("common.account")} isCollapsed={isCollapsed} isTextVisible={isTextVisible} />
        {renderSection(accountItems)}
      </div>
    </div>
  );
};
