import ClientWrapper from "@/components/ClientWrapper/ClientWrapper";
import { ReduxProvider } from "@/store/provider";
import { BASE_URL } from "@/Constant";
import type { Metadata } from "next";
import { ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import "./globals.css";

export const metadata: Metadata = {
  title: "IAAB",
  icons: {
    icon: "/favicon.ico",
  },
};

async function getWebsiteData() {
  try {
    const res = await fetch(
      `${BASE_URL}website/getWebsiteById?websiteId=66e3e5bfcdfd3a17049f0279`,
      {
        cache: "no-store", // 👈 debugging ke time
      }
    );

    if (!res.ok) {
      console.error("API status:", res.status);
      return {};
    }

    const json = await res.json();
    return json?.data ?? {};
  } catch (err: any) {
    console.error("Fetch failed:", err?.cause || err);
    return {};
  }
}

export default async function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const websiteData = await getWebsiteData();

  return (
    <html lang="en">
      <body>
        <ToastContainer position="top-right" autoClose={3000} />
        <ReduxProvider>
          <div className="bg-hero bg-cover pt-5">
            <ClientWrapper data={websiteData} children={children} />
          </div>
        </ReduxProvider>
      </body>
    </html>
  );
}
