/**
 * Pack-4 (session 65af6a29) ContentBriefs tests:
 *  - F4FE-011: results-count line is hidden while navigating (mirrors pagination block)
 */
import { render, screen } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';

const { mockNavigating } = vi.hoisted(() => ({
  mockNavigating: { value: false },
}));

vi.mock('@/hooks/useNavigationState', () => ({
  useNavigationState: () => mockNavigating.value,
}));

vi.mock('@inertiajs/react', async () => {
  const actual = await vi.importActual('@inertiajs/react');
  return {
    ...actual,
    usePage: vi.fn(() => ({
      props: {
        auth: { user: { name: 'Test User', email: 'test@example.com' } },
        features: { billing: false, notifications: false },
        sites: [],
        limits: null,
        ai_defaults: { model: 'gpt-4o-mini', temperature: 0.7 },
        polling_interval_ms: 5000,
      },
    })),
    Head: ({ title }: { title: string }) => <title>{title}</title>,
    Link: ({ children, href }: { children: React.ReactNode; href: string }) => (
      <a href={href}>{children}</a>
    ),
    router: { get: vi.fn(), post: vi.fn(), visit: vi.fn(), on: vi.fn(() => vi.fn()) },
  };
});

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

vi.mock('@/Components/theme/use-theme', () => ({
  useTheme: vi.fn(() => ({ theme: 'system', setTheme: vi.fn(), resolvedTheme: 'light' })),
}));

vi.mock('@/Layouts/DashboardLayout', () => ({
  default: ({ children }: { children: React.ReactNode }) => (
    <div data-testid="dashboard-layout">{children}</div>
  ),
}));

import Index from './Index';

const mockBrief = {
  id: 1,
  target_keyword: 'best running shoes',
  source_type: 'keyword_search',
  source_id: 1,
  status: 'completed',
  generated_at: '2026-01-01T00:00:00Z',
  created_at: '2026-01-01T00:00:00Z',
  suggested_publish_date: null,
};

const props = {
  site: { id: '1', name: 'Test Site', domain: 'example.com' },
  briefs: [mockBrief],
  filters: { status: null, source_type: null, sort: null },
  counts: {
    total: 1,
    pending: 0,
    generating: 0,
    completed: 1,
    failed: 0,
    TopicCluster: 0,
    TopicGapSuggestion: 0,
    FreshnessRecommendation: 0,
    CannibalizationCase: 0,
  },
  pagination: {
    current_page: 2,
    last_page: 4,
    per_page: 25,
    total: 95,
    links: [],
  },
  initial_keyword: '',
  // NEW-CI-007: required prop; true keeps existing test behaviour unchanged
  serp_key_available: true,
};

describe('ContentBriefs/Index – F4FE-011: results-count hidden while navigating', () => {
  it('shows the results count text ending in "briefs" when not navigating', () => {
    mockNavigating.value = false;
    render(<Index {...props} />);
    // The brief-count paragraph ends with "briefs" — find all and check at least one matches.
    const countParagraphs = screen.getAllByText(/\d+ briefs$/i);
    expect(countParagraphs.length).toBeGreaterThanOrEqual(1);
  });

  it('hides the results count paragraph while navigating (stale prop suppression)', () => {
    mockNavigating.value = true;
    render(<Index {...props} />);
    // While navigating, the "X briefs" count paragraph should NOT render.
    expect(screen.queryByText(/\d+ briefs$/i)).not.toBeInTheDocument();
  });
});
