import { Suspense } from "react";
import { NextResponse } from "next/server";
import { Center, Loader } from "@mantine/core";
import PageBody from "@/app/module-editor/[moduleId]/components/shared/PageBody/PageBody";
import { ModuleEditorContextProvider } from "@/app/module-editor/[moduleId]/contexts/ModuleEditorContext";
import PageContainer from "@/components/layout/PageContainer/PageContainer";

type Params = {
    params: Promise<{
        moduleId: string;
    }>;
};

export default async function ModuleEditorPage({ params }: Params) {
    const { moduleId } = await params;

    const id = Number(moduleId);
    if (isNaN(id)) {
        return NextResponse.json(
            { error: "Некорректный ID модуля. ID должен быть числом." },
            { status: 400 }
        );
    }

    return (
        <Suspense
            fallback={
                <Center h="50vh">
                    <Loader size="lg" />
                </Center>
            }>
            <PageContainer>
                <ModuleEditorContextProvider moduleId={id}>
                    <PageBody />
                </ModuleEditorContextProvider>
            </PageContainer>
        </Suspense>
    );
}
