import PositionSearch from "@/components/PositionSearch";
import { Metadata } from "next";
import { getServerI18n } from "@/i18n";
import Add from "@mui/icons-material/Add";
import Button from "@mui/material/Button";
import Stack from "@mui/material/Stack";
import Typography from "@mui/material/Typography";
import Link from "next/link";
import { Suspense } from "react";
import { fetchPositions, preloadPositions } from "@/app/api/positions";
import { fetchUserAbilities } from "@/app/utils/fetchUtil";
import { MAINTAIN_POSITION } from "@/middleware";

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

const Position: React.FC = async () => {
  const { t } = await getServerI18n("Position");
  
  // Preload necessary dependencies
  // fetchPositions();
  // preloadPositions();
  
  const abilities = await fetchUserAbilities()
  const maintainPosition = abilities.includes(MAINTAIN_POSITION)

  return (
    <>
      <Stack
        direction="row"
        justifyContent="space-between"
        flexWrap="wrap"
        rowGap={2}
      >
        <Typography variant="h4" marginInlineEnd={2}>
          {t("Position")}
        </Typography>
        {maintainPosition && <Button
          variant="contained"
          startIcon={<Add />}
          LinkComponent={Link}
          href="/settings/position/new"
        >
          {t("Create Position")}
        </Button>}
      </Stack>
      <Suspense fallback={<PositionSearch.Loading />}>
        <PositionSearch/>
      </Suspense>
    </>
  )
};

export default Position;
