import { describe, it, expect, afterEach } from 'vitest';
import { screen, waitFor } from '@testing-library/react';
import { EngagementsPage } from './EngagementsPage';
import { installFetchMock, renderWithProviders } from '@/test/testUtils';

describe('EngagementsPage', () => {
  let fetchMock: ReturnType<typeof installFetchMock>;

  afterEach(() => {
    fetchMock?.restore();
  });

  it('shows the loading row while the engagements query is pending', () => {
    fetchMock = installFetchMock([]); // never resolve in this test
    const pending: typeof fetch = () => new Promise(() => null);
    globalThis.fetch = pending;
    renderWithProviders(<EngagementsPage />);
    expect(screen.getByText(/fetching engagements/i)).toBeInTheDocument();
  });

  it('renders the empty state when the list is empty', async () => {
    fetchMock = installFetchMock([{ status: 200, body: [] }]);
    renderWithProviders(<EngagementsPage />);
    await screen.findByText(/no engagements yet/i);
  });

  it('renders the error state on 500', async () => {
    fetchMock = installFetchMock([
      { status: 500, body: { error: 'internal_error', message: 'boom' } },
    ]);
    renderWithProviders(<EngagementsPage />);
    await waitFor(() => {
      expect(screen.getByText(/fetch failed/i)).toBeInTheDocument();
    });
    expect(screen.getByRole('button', { name: /retry/i })).toBeInTheDocument();
  });

  it('renders rows when the backend returns engagements', async () => {
    fetchMock = installFetchMock([
      {
        status: 200,
        body: [
          {
            id: 'eng_1',
            client_name: 'Acme',
            description: null,
            status: 'active',
            c2_type: 'mythic',
            start_date: '2026-05-20',
            end_date: '2026-05-30',
          },
        ],
      },
    ]);
    renderWithProviders(<EngagementsPage />);
    await screen.findByText('Acme');
    expect(screen.getByText('MYTHIC')).toBeInTheDocument();
    expect(screen.getByText(/active/)).toBeInTheDocument();
  });
});
