import { TypeEnum } from "@/app/utils/typeEnum";
import RoughSchedule from "@/components/RoughSchedule";
import { getServerI18n, I18nProvider } 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 RoughScheduleDetailView from "@/components/RoughScheduleDetail";
import { SearchParams, ServerFetchError } from "@/app/utils/fetchUtil";
import { isArray, parseInt } from "lodash";
import { notFound } from "next/navigation";
import { fetchProdScheduleDetail } from "@/app/api/scheduling";

export const metadata: Metadata = {
    title: "Demand Forecast Detail",
};

type Props = SearchParams

const roughSchedulingDetail: React.FC<Props> = async ({ searchParams }) => {
    const { t } = await getServerI18n("schedule");
    const id = searchParams["id"]
    const type = "rough"

    if (!id || isArray(id) || !isFinite(parseInt(id))) {
        notFound()
    }

    try {
        await fetchProdScheduleDetail(parseInt(id))
    } catch(e) {
        if (e instanceof ServerFetchError && (e.response?.status === 404 || e.response?.status === 400)) {
            console.log(e)
            notFound();
        }
    }
    //   preloadClaims();

    return (
        <>
            <Stack
                direction="row"
                justifyContent="space-between"
                flexWrap="wrap"
                rowGap={2}
            >
                <Typography variant="h4" marginInlineEnd={2}>
                    {t("FG & Material Demand Forecast Detail")}
                </Typography>
                {/* <Button
          variant="contained"
          startIcon={<Add />}
          LinkComponent={Link}
          href="product/create"
        >
          {t("Create product")}
        </Button> */}
            </Stack>
            <I18nProvider namespaces={["schedule","common"]}>
                <Suspense fallback={<RoughScheduleDetailView.Loading />}>
                    <RoughScheduleDetailView type={type} id={parseInt(id)}/>
                </Suspense>
            </I18nProvider>
        </>
    );
};

export default roughSchedulingDetail;
