import { preloadTaskTemplates } from "@/app/api/tasks";
import TaskTemplateSearch from "@/components/TaskTemplateSearch";
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 { Metadata } from "next";
import Link from "next/link";
import { Suspense } from "react";
import { I18nProvider } from "@/i18n";

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

const TaskTemplates: React.FC = async () => {
  const { t } = await getServerI18n("tasks");
  preloadTaskTemplates();

  return (
    <>
      <Stack
        direction="row"
        justifyContent="space-between"
        flexWrap="wrap"
        rowGap={2}
      >
        <Typography variant="h4" marginInlineEnd={2}>
          {t("Task Template")}
        </Typography>
        <Button
          variant="contained"
          startIcon={<Add />}
          LinkComponent={Link}
          href="/tasks/create"
        >
          {t("Create Task Template")}
        </Button>
      </Stack>
      <I18nProvider namespaces={["tasks", "common"]}>
        <Suspense fallback={<TaskTemplateSearch.Loading />}>
          <TaskTemplateSearch />
        </Suspense>
      </I18nProvider>
    </>
  );
};

export default TaskTemplates;
