import { preloadAllCustomers } from "@/app/api/customer";
import CustomerSearch from "@/components/CustomerSearch";
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";
import { fetchUserAbilities } from "@/app/utils/fetchUtil";
import { MAINTAIN_CLIENT } from "@/middleware";

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

const Customer: React.FC = async () => {
    const { t } = await getServerI18n("customer");
    preloadAllCustomers();
    const abilities = await fetchUserAbilities()
    const maintainClient = abilities.includes(MAINTAIN_CLIENT)

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

export default Customer;
