import { render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

import { CTA_LABELS } from '@/config/cta-labels';
import { trackEvent } from '@/lib/analytics';

import { OnboardingUpgradeCard } from './OnboardingUpgradeCard';

vi.mock('@/lib/analytics', () => ({
  trackEvent: vi.fn(),
}));

// BillingLink navigates externally; stub it so tests don't need a billing env.
vi.mock('@/Components/billing/BillingLink', () => ({
  BillingLink: ({ children, onClick }: { children: React.ReactNode; onClick?: () => void }) => (
    <a href="#billing" onClick={onClick}>
      {children}
    </a>
  ),
}));

describe('OnboardingUpgradeCard', () => {
  it('renders the recommendations-count headline when count is positive', () => {
    render(<OnboardingUpgradeCard siteId={'1'} recommendationsCount={7} />);
    expect(screen.getByText('Want to fix all 7?')).toBeInTheDocument();
  });

  it('renders the generic headline when recommendations count is zero', () => {
    render(<OnboardingUpgradeCard siteId={'1'} recommendationsCount={0} />);
    expect(screen.getByText('Try Pro free for 14 days.')).toBeInTheDocument();
  });

  it('renders the ROI-climax feature description (ONBOARD-001 / AB-04)', () => {
    render(<OnboardingUpgradeCard siteId={'1'} recommendationsCount={3} />);
    expect(
      screen.getByText(/prove which fixes drove lift/i),
    ).toBeInTheDocument();
    expect(screen.getByText(/before-after ROI on every recommendation/i)).toBeInTheDocument();
  });

  it('renders the proof-preview callout with illustrative KPIs (EMAIL-003)', () => {
    render(<OnboardingUpgradeCard siteId={'1'} recommendationsCount={3} />);

    expect(screen.getByText(/example ROI report — illustrative numbers/i)).toBeInTheDocument();
    expect(screen.getByText('+93 clicks')).toBeInTheDocument();
    expect(screen.getByText('Position 6.2 → 4.1')).toBeInTheDocument();
    expect(screen.getByText('CTR up 1.8%')).toBeInTheDocument();
    expect(
      screen.getByText(/your actual report pulls the same metrics directly from GSC/i),
    ).toBeInTheDocument();
  });

  it('renders the trial CTA using the canonical CTA_LABELS.PRO_TRIAL string', () => {
    render(<OnboardingUpgradeCard siteId={'1'} recommendationsCount={3} />);
    expect(screen.getByRole('link', { name: CTA_LABELS.PRO_TRIAL })).toBeInTheDocument();
  });

  it('fires UPGRADE_PROMPT_CLICKED with correct payload on CTA click', async () => {
    const { UPGRADE_PROMPT_CLICKED } = await import('@/lib/event-catalog');
    render(<OnboardingUpgradeCard siteId={'42'} recommendationsCount={5} />);

    screen.getByRole('link', { name: CTA_LABELS.PRO_TRIAL }).click();

    expect(vi.mocked(trackEvent)).toHaveBeenCalledWith(
      UPGRADE_PROMPT_CLICKED,
      expect.objectContaining({
        surface: 'onboarding_wizard',
        source: 'onboarding_analysis_complete',
        site_id: '42',
      }),
    );
  });
});
