import { Metadata } from "next"

import FeaturedProducts from "@modules/home/components/featured-products"
import Hero from "@modules/home/components/hero"
import { listCollections } from "@lib/data/collections"
import { getRegion } from "@lib/data/regions"
import VtFeaturedProducts from "@modules/home/components/vt-featured-products"
import { DynamicLayoutRenderer } from "@vibentec/renderer"
import { LayoutContext, LayoutComponentNode } from "@vibentec/component-map"
import { loadPageConfig } from "@vibentec/configloader"

export const metadata: Metadata = {
  title: "Medusa Next.js Starter Template",
  description:
    "A performant frontend ecommerce starter template with Next.js 15 and Medusa.",
}

export default async function Home(props: {
  params: Promise<{ countryCode: string }>
}) {
  const params = await props.params

  const { countryCode } = params

  const region = await getRegion(countryCode)

  const { collections } = await listCollections({
    fields: "id, handle, title",
  })

  console.log('collections:',collections)

  if (!collections || !region) {
    return null
  }
  const nodes: LayoutComponentNode[] = await loadPageConfig("Home")

  if (!region) {
    return null
  }

  const context: LayoutContext = {
    customer: null,
    cart: null,
    shippingOptions: [],
    contentChildren: null,
    countryCode,
    region,
  }

  return <DynamicLayoutRenderer nodes={nodes} context={context} />
}
