/**
 * Tests for Optimizer Dashboard
 */
import { render, screen, waitFor } from '@testing-library/react';
import { BrowserRouter } from 'react-router';
import OptimizerDashboard 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('OptimizerDashboard', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

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

  it('displays stats cards when data is loaded', async () => {
    (fetchContent as any).mockResolvedValue({
      results: [
        { id: 1, optimizer_version: 1, optimization_scores: { overall_score: 75 } },
        { id: 2, optimizer_version: 0 },
      ],
      count: 2,
    });
    
    render(
      <BrowserRouter>
        <OptimizerDashboard />
      </BrowserRouter>
    );
    
    await waitFor(() => {
      expect(screen.getByText('Total Optimized')).toBeInTheDocument();
      expect(screen.getByText('Avg Score Improvement')).toBeInTheDocument();
    });
  });

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

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

