import { getTranslations } from "next-intl/server";
import { getConsumerGroup } from "@/api/groups/actions";
import { GroupParams } from "@/app/[locale]/(authorized)/kafka/[kafkaId]/groups/[groupId]/Group.params";
import { MembersTable } from "@/app/[locale]/(authorized)/kafka/[kafkaId]/groups/[groupId]/members/MembersTable";
import { KafkaParams } from "@/app/[locale]/(authorized)/kafka/[kafkaId]/kafka.params";
import { PageSection } from "@/libs/patternfly/react-core";
import { Suspense } from "react";
import { NoDataErrorState } from "@/components/NoDataErrorState";

export async function generateMetadata({
  params: paramsPromise,
} : {
  params: Promise<GroupParams>;
}) {
  const { kafkaId, groupId } = await paramsPromise;
  const t = await getTranslations();
  const group = (await getConsumerGroup(kafkaId, groupId)).payload;
  let groupIdDisplay = "";

  if (group) {
    groupIdDisplay = group.attributes.groupId;
  }

  return {
    title: `${t("Group.title")} ${groupIdDisplay} | ${t("common.title")}`,
  };
}

export default async function ConsumerGroupMembersPage({
  params: paramsPromise,
}: {
  params: Promise<GroupParams>;
}) {
  const { kafkaId, groupId } = await paramsPromise;
  return (
    <PageSection>
      <Suspense
        fallback={<MembersTable kafkaId={kafkaId} consumerGroup={undefined} />}
      >
        <ConnectedMembersTable params={{ kafkaId, groupId }} />
      </Suspense>
    </PageSection>
  );
}

async function ConnectedMembersTable({
  params: { kafkaId, groupId },
}: {
  params: KafkaParams & { groupId: string };
}) {
  const response = await getConsumerGroup(kafkaId, groupId);

  if (response.errors) {
    return <NoDataErrorState errors={response.errors} />;
  }

  const consumerGroup = response.payload!;
  return <MembersTable kafkaId={kafkaId} consumerGroup={consumerGroup} />;
}
