import posthog from 'posthog-js'

// Export posthog instance for use throughout the app
export { posthog }

// Helper function to identify users
export const identifyUser = (userId: string, properties?: Record<string, any>) => {
  posthog.identify(userId, properties)
}

// Helper function to capture custom events
export const captureEvent = (eventName: string, properties?: Record<string, any>) => {
  posthog.capture(eventName, properties)
}

// Helper function to reset user data (for logout)
export const resetPostHog = () => {
  posthog.reset()
}

// Helper function to set user properties
export const setUserProperties = (properties: Record<string, any>) => {
  posthog.people.set(properties)
}

// Helper function to alias anonymous user to identified user
export const aliasUser = (distinctId: string) => {
  posthog.alias(distinctId)
}

// Page view tracking
export const capturePageView = (pageName?: string, properties?: Record<string, any>) => {
  posthog.capture('$pageview', {
    page: pageName || window.location.pathname,
    ...properties
  })
}

// Revenue tracking
export const captureRevenue = (amount: number, properties?: Record<string, any>) => {
  posthog.capture('$transaction', {
    $amount: amount,
    ...properties
  })
}