import { Metadata } from "next";
import { getServerI18n, I18nProvider } from "@/i18n";
import Typography from "@mui/material/Typography";
import { fetchQcCategoryDetails, preloadQcCategory } from "@/app/api/settings/qcCategory";
import QcCategorySave from "@/components/QcCategorySave";
import { isArray } from "lodash";
import { notFound } from "next/navigation";
import { ServerFetchError } from "@/app/utils/fetchUtil";

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

interface Props {
  searchParams: { [key: string]: string | string[] | undefined };
}

const qcCategory: React.FC<Props> = async ({ searchParams }) => {
  const { t } = await getServerI18n("qcCategory");

  const id = searchParams["id"];

  if (!id || isArray(id)) {
    notFound();
  }

  try {
    console.log("first");
    await fetchQcCategoryDetails(id);
    console.log("firsts");
  } catch (e) {
    if (
      e instanceof ServerFetchError &&
      (e.response?.status === 404 || e.response?.status === 400)
    ) {
      console.log(e);
      notFound();
    }
  }

  return (
    <>
      <Typography variant="h4" marginInlineEnd={2}>
        {t("Edit Qc Category")}
      </Typography>
      <I18nProvider namespaces={["qcCategory","navigation","common"]}>
        <QcCategorySave id={id} />
      </I18nProvider>
    </>
  );
};

export default qcCategory;
