import { I18nProvider, getServerI18n } from "@/i18n";
import ItemTracing from "@/components/ItemTracing";
import { AUTH } from "@/authorities";
import { authOptions } from "@/config/authConfig";
import { Stack, Typography } from "@mui/material";
import { Metadata } from "next";
import { getServerSession } from "next-auth";
import { redirect } from "next/navigation";
import { Suspense } from "react";
import ItemTracingLoading from "@/components/ItemTracing/ItemTracingLoading";

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

const canAccess = (abilities: string[]) =>
  abilities.includes(AUTH.ITEM_TRACING);

const ItemTracingPage: React.FC = async () => {
  const session = await getServerSession(authOptions);
  const abilities = session?.user?.abilities ?? [];
  if (!canAccess(abilities)) {
    redirect("/dashboard");
  }

  const { t } = await getServerI18n("itemTracing");

  return (
    <>
      <Stack direction="row" justifyContent="space-between" flexWrap="wrap" rowGap={2}>
        <Typography variant="h4" marginInlineEnd={2}>
          {t("title")}
        </Typography>
      </Stack>
      <I18nProvider namespaces={["itemTracing", "navigation", "common"]}>
        <Suspense fallback={<ItemTracingLoading />}>
          <ItemTracing />
        </Suspense>
      </I18nProvider>
    </>
  );
};

export default ItemTracingPage;
