import { notFound } from "next/navigation";
import fs from "node:fs/promises";
import path from "node:path";

import { request } from "@/lib/request";
import { GetCurrentBlockPage } from "@/lib/graphql/Query/CurrentBlockPage.graphql";

import type { PageArgs, LangParams, UriParams } from "@/types/params";
import type { GetCurrentBlockPageData } from "@/types/requests/currentBlockPage";

export default async function CurrentContentPage({
  params,
  searchParams,
}: PageArgs<LangParams, UriParams>) {
  try {
    console.log({ searchParams });
    if (!searchParams.uri) {
      throw new Error("Bad URI");
    }

    // const response = await request<GetCurrentBlockPageData>(GetCurrentBlockPage, { locale: params.lang, url: searchParams.uri })

    // console.log(response.data.all_current_blocks_page.items[0].blocks)

    const filePath = path.join(
      process.cwd(),
      "mockCms",
      params.lang,
      searchParams.uri,
      "data.json"
    );

    const data = await fs.readFile(filePath, { encoding: "utf-8" });

    if (!data) {
      throw new Error("No data");
    }

    const json = JSON.parse(data);

    return (
      <>
        {json.hero ? (
          <div
            dangerouslySetInnerHTML={{ __html: json.hero }}
            className="hero-content-overlay hero-content-widget"
          />
        ) : null}
        {json.content ? (
          <main
            className="main l-sections-wrapper"
            role="main"
            id="maincontent"
            dangerouslySetInnerHTML={{ __html: json.content }}
          />
        ) : null}
      </>
    );
  } catch (err) {
    return notFound();
  }
}
