import type { PageProps } from "@/core/types/common";
import type { Metadata, ResolvingMetadata, ServerRuntime } from "next";

import Logger from "@/core/utils/helpers/logger";
import dynamic from "next/dynamic";

import RedirectProvider from "@/core/common/RedirectProvider";

const DynamicUnityComponent = dynamic(() => import("@/core/common/Unity"));

type Params = { modelId: string };

export const runtime: ServerRuntime = "nodejs";

export async function generateMetadata(props: PageProps<Params>,
                                       parent: ResolvingMetadata): Promise<Metadata> {
  const id = props.params.modelId;
  console.log("Generating Metadata...");
  return {
    metadataBase: new URL("http://localhost:3000"),
    title: `3D Model | Rendersee`,
    description: "Interactive 3D building model viewer. Customization of the interior.",
  }
}

export default async function ModelViewer(props: PageProps<Params>): Promise<JSX.Element> {

  try {
    return (
      <RedirectProvider sourceKey={"customization_source"}>
        <DynamicUnityComponent id={props.params.modelId} />
      </RedirectProvider>);
  } catch (error: any) {
    Logger.log("Error loading 3D model's page: ", error);
    throw new Error("Unity build not found: ", error);
  }
}