/**
 * Tests for Linker Dashboard
 */
import { render, screen, waitFor } from '@testing-library/react';
import { BrowserRouter } from 'react-router';
import LinkerDashboard from '../Dashboard';
import { fetchContent } from '../../../services/api';

vi.mock('../../../services/api');
vi.mock('../../../store/siteStore', () => ({
  useSiteStore: () => ({ activeSite: { id: 1, name: 'Test Site' } }),
}));
vi.mock('../../../store/sectorStore', () => ({
  useSectorStore: () => ({ activeSector: { id: 1, name: 'Test Sector' } }),
}));

describe('LinkerDashboard', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('renders dashboard title', () => {
    (fetchContent as any).mockResolvedValue({ results: [], count: 0 });
    
    render(
      <BrowserRouter>
        <LinkerDashboard />
      </BrowserRouter>
    );
    
    expect(screen.getByText('Linker Dashboard')).toBeInTheDocument();
  });

  it('displays stats cards when data is loaded', async () => {
    (fetchContent as any).mockResolvedValue({
      results: [
        { id: 1, internal_links: [{ id: 1 }] },
        { id: 2, internal_links: [] },
      ],
      count: 2,
    });
    
    render(
      <BrowserRouter>
        <LinkerDashboard />
      </BrowserRouter>
    );
    
    await waitFor(() => {
      expect(screen.getByText('Total Linked')).toBeInTheDocument();
      expect(screen.getByText('Total Links')).toBeInTheDocument();
    });
  });

  it('shows loading state initially', () => {
    (fetchContent as any).mockImplementation(() => new Promise(() => {}));
    
    render(
      <BrowserRouter>
        <LinkerDashboard />
      </BrowserRouter>
    );
    
    expect(screen.getByText('Loading stats...')).toBeInTheDocument();
  });

  it('renders quick actions', async () => {
    (fetchContent as any).mockResolvedValue({ results: [], count: 0 });
    
    render(
      <BrowserRouter>
        <LinkerDashboard />
      </BrowserRouter>
    );
    
    await waitFor(() => {
      expect(screen.getByText('Link Content')).toBeInTheDocument();
      expect(screen.getByText('View Content')).toBeInTheDocument();
    });
  });
});

