import { usePathname, useSearchParams } from 'solito/navigation'
import { getBaseDomain, ProductProvider, useProduct } from '@junwon/config'
import { GenerativePage } from '@junwon/generation-tech/components/GenerativePage'

import { HomePage } from '@junwon/catalog-tech/pages/HomePage'
import { SearchOutputPage } from '@junwon/catalog-tech/pages/SearchOutputPage'

interface PageRouterProps {
  productName: string
}

export function PageRouter({ productName }: PageRouterProps) {
  return (
    <ProductProvider productName={productName}>
      <Page />
    </ProductProvider>
  )
}

function Page() {
  const { productName } = useProduct()
  const baseDomain = getBaseDomain(productName)
  const pathname = usePathname() ?? '/'
  const searchParams = useSearchParams()?.toString() ?? ''
  const pageLink = `${baseDomain}${pathname}${searchParams}`

  switch (pathname) {
    case '/':
      return <HomePage />
    case '/search':
      return <SearchOutputPage />
    default:
      return (
        <GenerativePage
          pageSpec={`Generate a web page that fits the web browser address bar: ${pageLink}. \n`}
        />
      )
  }
}
