import { describe, it, expect, beforeEach, beforeAll, vi } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import { MemoryRouter } from 'react-router-dom'
import { HelmetProvider } from 'react-helmet-async'

import LandingPage from '../LandingPage'
import {
  __resetAppConfigCache,
  __setAppConfigCache,
} from '@/hooks/useAppConfig'

// jsdom does not provide IntersectionObserver. LandingPage uses it for
// scroll-reveal animations; stub a no-op so the page can mount.
beforeAll(() => {
  // @ts-expect-error — test-only stub
  globalThis.IntersectionObserver = class {
    observe() {}
    unobserve() {}
    disconnect() {}
    takeRecords() {
      return []
    }
  }
})

function renderPage() {
  return render(
    <HelmetProvider>
      <MemoryRouter initialEntries={['/']}>
        <LandingPage />
      </MemoryRouter>
    </HelmetProvider>,
  )
}

describe('LandingPage', () => {
  beforeEach(() => {
    __resetAppConfigCache()
    vi.clearAllMocks()
  })

  it('shows See pricing CTA when self_serve_enabled is true', async () => {
    __setAppConfigCache({
      self_serve_enabled: true,
      oauth_providers: [],
    })

    renderPage()

    await waitFor(() => {
      expect(screen.getByTestId('landing-see-pricing')).toBeInTheDocument()
    })
    const cta = screen.getByTestId('landing-see-pricing')
    expect(cta).toHaveAttribute('href', '/pricing')
    expect(cta).toHaveTextContent(/See pricing/i)
  })

  it('hides See pricing CTA when self_serve_enabled is false', async () => {
    __setAppConfigCache({
      self_serve_enabled: false,
      oauth_providers: [],
    })

    renderPage()

    // Hero "Start Free" still renders, but the gated /pricing CTA does not.
    expect(screen.queryByTestId('landing-see-pricing')).not.toBeInTheDocument()
  })
})
