/**
 * DashboardBanners — useDashboardBanners hook unit tests.
 * INTUX-101 QA-003: verify wp-degraded banner branch is exercised.
 */
import { render, renderHook, screen } from '@testing-library/react';

import { useDashboardBanners, type UseDashboardBannersOptions } from './DashboardBanners';

const baseOptions: UseDashboardBannersOptions = {
  userId: 1,
  firstSite: { id: '1', name: 'Test Site' },
  gscSyncing: false,
  gscConnectedNoRun: false,
  gscExpiringConnections: [],
  wpDegradedConnections: [],
  wizardCompleted: true,
  isActivated: true,
  hasReviewedRecommendation: false,
  onboardingFeatureEnabled: false,
  streakMilestoneNotification: null,
  dashboardMetrics: null,
  requestSyncNotificationPermission: () => {},
};

describe('useDashboardBanners — wp-degraded banner (INTUX-101)', () => {
  it('adds wp-degraded banner at connection priority when a WP connection is auth_failed', () => {
    const { result } = renderHook(() =>
      useDashboardBanners({
        ...baseOptions,
        wpDegradedConnections: [{ site_id: '01JWMTEST00000000000000001', site_name: 'My Blog', status: 'auth_failed' }],
      }),
    );

    const banner = result.current.find((b) => b.key === 'wp-degraded');
    expect(banner).toBeDefined();
    expect(banner?.priority).toBe('connection');
  });

  it('renders auth_failed copy and Reconnect CTA', () => {
    const { result } = renderHook(() =>
      useDashboardBanners({
        ...baseOptions,
        wpDegradedConnections: [{ site_id: '01JWMTEST00000000000000001', site_name: 'My Blog', status: 'auth_failed' }],
      }),
    );

    const banner = result.current.find((b) => b.key === 'wp-degraded');
    render(banner!.render());

    expect(screen.getByText(/WordPress connection broken/)).toBeInTheDocument();
    expect(screen.getByText(/Authentication failed/)).toBeInTheDocument();
    expect(screen.getByRole('link', { name: /Reconnect WordPress/i })).toBeInTheDocument();
  });

  it('renders unreachable copy when WP connection status is unreachable', () => {
    const { result } = renderHook(() =>
      useDashboardBanners({
        ...baseOptions,
        wpDegradedConnections: [{ site_id: '01JWMTEST00000000000000002', site_name: 'My Store', status: 'unreachable' }],
      }),
    );

    const banner = result.current.find((b) => b.key === 'wp-degraded');
    render(banner!.render());

    expect(screen.getByText(/WordPress connection broken/)).toBeInTheDocument();
    expect(screen.getByText(/unreachable/i)).toBeInTheDocument();
  });

  it('returns no wp-degraded banner when wpDegradedConnections is empty', () => {
    const { result } = renderHook(() =>
      useDashboardBanners({ ...baseOptions, wpDegradedConnections: [] }),
    );

    const banner = result.current.find((b) => b.key === 'wp-degraded');
    expect(banner).toBeUndefined();
  });

  it('returns no wp-degraded banner when prop is omitted', () => {
    const options = { ...baseOptions } as UseDashboardBannersOptions;
    delete options.wpDegradedConnections;
    const { result } = renderHook(() => useDashboardBanners(options));

    const banner = result.current.find((b) => b.key === 'wp-degraded');
    expect(banner).toBeUndefined();
  });
});
