import SearchBar from "@/components/SearchBar";
import useCurrentUser from "@/hooks/useCurrentUser";
import { cn } from "@/lib/utils";
import type { StatisticsData } from "@/types/statistics";
import StatisticsView from "../StatisticsView";
import BookmarksSection from "./BookmarksSection";
import ShortcutsSection from "./ShortcutsSection";
import TagsSection from "./TagsSection";

export type MemoExplorerContext = "home" | "explore" | "archived" | "profile";

export interface MemoExplorerFeatures {
  search?: boolean;
  statistics?: boolean;
  bookmarks?: boolean;
  shortcuts?: boolean;
  tags?: boolean;
}

interface Props {
  className?: string;
  context?: MemoExplorerContext;
  features?: MemoExplorerFeatures;
  statisticsData: StatisticsData;
  tagCount: Record<string, number>;
}

const getDefaultFeatures = (context: MemoExplorerContext): MemoExplorerFeatures => {
  switch (context) {
    case "explore":
      return {
        search: true,
        statistics: true,
        bookmarks: false, // No bookmarks in explore
        shortcuts: true,
        tags: true,
      };
    case "archived":
      return {
        search: true,
        statistics: true,
        bookmarks: false, // No bookmarks in archived
        shortcuts: true,
        tags: true,
      };
    case "profile":
      return {
        search: true,
        statistics: true,
        bookmarks: false, // No bookmarks in profile view
        shortcuts: true,
        tags: true,
      };
    case "home":
    default:
      return {
        search: true,
        statistics: true,
        bookmarks: true, // Bookmarks in home
        shortcuts: true,
        tags: true,
      };
  }
};

const MemoExplorer = (props: Props) => {
  const { className, context = "home", features: featureOverrides = {}, statisticsData, tagCount } = props;
  const currentUser = useCurrentUser();

  // Merge default features with overrides
  const features = {
    ...getDefaultFeatures(context),
    ...featureOverrides,
  };

  return (
    <aside
      className={cn(
        "relative w-full h-full overflow-auto flex flex-col justify-start items-start bg-background text-sidebar-foreground",
        className,
      )}
    >
      {features.search && <SearchBar />}
      <div className="mt-1 px-1 w-full">
        {features.statistics && <StatisticsView statisticsData={statisticsData} />}
        {features.bookmarks && <BookmarksSection className="mt-2" />}
        {features.shortcuts && <ShortcutsSection />}
        {features.tags && <TagsSection readonly={context === "explore"} tagCount={tagCount} />}
      </div>
    </aside>
  );
};

export default MemoExplorer;
