import type { Metadata } from "next";
import { Suspense } from "react";
import { getTranslations } from "next-intl/server";
import { cookies } from "next/headers";
import ContentBlock from "@/components/ContentBlock";
import { LoadingSpinner } from "@/components/LoadingSpinner";
import SearchInput from "@/components/SearchInput";
import MCPHeaderTabs from "./components/MCPHeaderTabs";
import MCPServersContent from "./components/MCPServersContent";
import { AddConnectionDropdown } from "./components/AddConnectionDropdown";

export const metadata: Metadata = {
  title: "Connections",
};

export default async function MCPServersPage({
  searchParams,
}: {
  searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
}) {
  const t = await getTranslations("MCPServersPage");
  const resolvedSearchParams = await searchParams;

  // Read tab from URL or fallback to cookie
  const cookieStore = await cookies();
  const cookieTab = cookieStore.get("tab_mcp-servers")?.value;
  const tab =
    typeof resolvedSearchParams.tab === "string"
      ? resolvedSearchParams.tab
      : cookieTab || "grid";
  const searchQuery =
    typeof resolvedSearchParams.search === "string"
      ? resolvedSearchParams.search
      : "";
  const typeFilter =
    typeof resolvedSearchParams.type === "string"
      ? resolvedSearchParams.type
      : "";
  const categoryFilter =
    typeof resolvedSearchParams.category === "string"
      ? resolvedSearchParams.category
      : "";

  return (
    <ContentBlock
      header={{
        breadcrumb: [{ label: t("title") }],
        description: t("description"),
        controls: <AddConnectionDropdown />,
      }}
      subheader={
        <>
          <SearchInput urlParamName="search" urlPath="/mcp-servers" />
          <MCPHeaderTabs currentTab={tab} />
        </>
      }
    >
      <Suspense
        key={`${searchQuery}-${tab}-${typeFilter}-${categoryFilter}`}
        fallback={
          <div className="flex h-32 items-center justify-center">
            <LoadingSpinner />
          </div>
        }
      >
        <MCPServersContent searchQuery={searchQuery} viewMode={tab} typeFilter={typeFilter} categoryFilter={categoryFilter} />
      </Suspense>
    </ContentBlock>
  );
}
