import { describe, it, expect, vi, beforeEach } from 'vitest';
import { screen, waitFor } from '../../../test/utils/test-utils';
import { renderWithRouter } from '../../../test/utils/test-utils';
import DashboardPage from '../DashboardPage';

// Mock useNavigate
const mockNavigate = vi.fn();
vi.mock('react-router-dom', async () => {
  const actual = await vi.importActual('react-router-dom');
  return {
    ...actual,
    useNavigate: () => mockNavigate,
  };
});

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

  it('should render loading state initially', () => {
    renderWithRouter(<DashboardPage />);
    expect(screen.getByText(/Loading/i)).toBeInTheDocument();
  });

  it('should fetch and display dashboard stats', async () => {
    renderWithRouter(<DashboardPage />);

    await waitFor(() => {
      expect(screen.queryByText(/Loading/i)).not.toBeInTheDocument();
    }, { timeout: 5000 });

    // Check if stats are displayed
    await waitFor(() => {
      expect(screen.getByText(/Total Revenue/i)).toBeInTheDocument();
    });
  });

  it('should display financial summary', async () => {
    renderWithRouter(<DashboardPage />);

    await waitFor(() => {
      expect(screen.queryByText(/Loading/i)).not.toBeInTheDocument();
    }, { timeout: 5000 });

    // Financial summary should be present
    await waitFor(() => {
      const summarySection = screen.queryByText(/Financial Summary/i);
      if (summarySection) {
        expect(summarySection).toBeInTheDocument();
      }
    });
  });

  it('should display recent invoices', async () => {
    renderWithRouter(<DashboardPage />);

    await waitFor(() => {
      expect(screen.queryByText(/Loading/i)).not.toBeInTheDocument();
    }, { timeout: 5000 });

    // Invoices section should be present
    await waitFor(() => {
      const invoicesSection = screen.queryByText(/Recent Invoices/i);
      if (invoicesSection) {
        expect(invoicesSection).toBeInTheDocument();
      }
    });
  });
});

