'use client'

import { useEffect } from 'react'
import { usePathname } from 'next/navigation'
import posthog from 'posthog-js'

export default function PostHogProvider({ children }: { children: React.ReactNode }) {
  useEffect(() => {
    posthog.init('phc_BHgg9S7CQqVShe7EMCdi86PxA49qcNaTsR9Nn5EGxRCT', {
      api_host: 'https://eu.posthog.com',
      capture_pageview: false,
      persistence: 'memory',
    })
  }, [])

  const pathname = usePathname()

  useEffect(() => {
    // PostHog queues events automatically before init completes
    posthog.capture('$pageview', { $current_url: window.location.href })
  }, [pathname])

  return <>{children}</>
}
