import { preloadClaims } from "@/app/api/claims";
import { preloadStaff, preloadTeamLeads } from "@/app/api/staff";
import { fetchUserAbilities } from "@/app/utils/fetchUtil";
import StaffSearch from "@/components/StaffSearch";
import TeamSearch from "@/components/TeamSearch";
import { I18nProvider, getServerI18n } from "@/i18n";
import { MAINTAIN_TEAM } from "@/middleware";
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 { Metadata } from "next";
import Link from "next/link";
import { Suspense } from "react";


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


  const Team: React.FC = async () => {
    const { t } = await getServerI18n("team");
    // preloadTeamLeads();
    // preloadStaff();

    const abilities = await fetchUserAbilities()
    const maintainTeam = abilities.includes(MAINTAIN_TEAM)

    return (
      <>
        <Stack
          direction="row"
          justifyContent="space-between"
          flexWrap="wrap"
          rowGap={2}
        >
          <Typography variant="h4" marginInlineEnd={2}>
            {t("Team")}
          </Typography>
          {maintainTeam && <Button
            variant="contained"
            startIcon={<Add />}
            LinkComponent={Link}
            href="/settings/team/create"
          >
            {t("Create Team")}
          </Button>}
        </Stack>
        <I18nProvider namespaces={["team", "common"]}>
          <Suspense fallback={<TeamSearch.Loading />}>
            <TeamSearch />
          </Suspense>
        </I18nProvider>
      </>
    );
  };
  
  export default Team;