import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'

vi.mock('posthog-react-native', () => {
  const mockPostHog = {
    capture: vi.fn(),
    identify: vi.fn(),
    setPersonProperties: vi.fn(),
    startSessionRecording: vi.fn(),
    stopSessionRecording: vi.fn(),
    isSessionReplayActive: vi.fn().mockResolvedValue(true),
  }
  
  return {
    default: vi.fn().mockImplementation(() => mockPostHog),
  }
})

describe('analytics service', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    vi.resetModules()
  })

  afterEach(() => {
    vi.restoreAllMocks()
  })

  describe('initializeAnalytics', () => {
    it('should initialize PostHog client', async () => {
      const { initializeAnalytics } = await import('../../shared/services/analytics')
      
      const client = await initializeAnalytics()
      
      expect(client).toBeDefined()
    })

    it('should return existing client if already initialized', async () => {
      const { initializeAnalytics } = await import('../../shared/services/analytics')
      
      const client1 = await initializeAnalytics()
      const client2 = await initializeAnalytics()
      
      expect(client1).toBe(client2)
    })
  })

  describe('getPostHogClient', () => {
    it('should return null before initialization', async () => {
      vi.resetModules()
      const { getPostHogClient } = await import('../../shared/services/analytics')
      
      expect(getPostHogClient()).toBeNull()
    })

    it('should return client after initialization', async () => {
      vi.resetModules()
      const { initializeAnalytics, getPostHogClient } = await import('../../shared/services/analytics')
      
      await initializeAnalytics()
      
      expect(getPostHogClient()).toBeDefined()
    })
  })

  describe('track', () => {
    it('should track event with properties', async () => {
      const { initializeAnalytics, track } = await import('../../shared/services/analytics')
      
      await initializeAnalytics()
      track('test_event', { prop1: 'value1' })
      
    })

    it('should track event without properties', async () => {
      const { initializeAnalytics, track } = await import('../../shared/services/analytics')
      
      await initializeAnalytics()
      track('test_event')
      
    })
  })

  describe('trackScreen', () => {
    it('should track screen view', async () => {
      const { initializeAnalytics, trackScreen } = await import('../../shared/services/analytics')
      
      await initializeAnalytics()
      trackScreen('home', { source: 'tab' })
      
    })
  })

  describe('identifyUser', () => {
    it('should identify user with traits', async () => {
      const { initializeAnalytics, identifyUser } = await import('../../shared/services/analytics')
      
      await initializeAnalytics()
      identifyUser('user-123', { name: 'Test User' })
      
    })

    it('should identify user without traits', async () => {
      const { initializeAnalytics, identifyUser } = await import('../../shared/services/analytics')
      
      await initializeAnalytics()
      identifyUser('user-123')
      
    })
  })

  describe('setUserProperties', () => {
    it('should set user properties', async () => {
      const { initializeAnalytics, setUserProperties } = await import('../../shared/services/analytics')
      
      await initializeAnalytics()
      setUserProperties({ plan: 'pro', workouts: 10 })
      
    })
  })

  describe('session recording', () => {
    it('should start session recording', async () => {
      const { initializeAnalytics, startSessionRecording } = await import('../../shared/services/analytics')
      
      await initializeAnalytics()
      startSessionRecording()
      
    })

    it('should stop session recording', async () => {
      const { initializeAnalytics, stopSessionRecording } = await import('../../shared/services/analytics')
      
      await initializeAnalytics()
      stopSessionRecording()
      
    })

    it('should check session replay status', async () => {
      const { initializeAnalytics, isSessionReplayActive } = await import('../../shared/services/analytics')
      
      await initializeAnalytics()
      const isActive = await isSessionReplayActive()
      
      expect(typeof isActive).toBe('boolean')
    })
  })
})
