/**
 * @ai-summary Tests for AdminUsersPage component
 */

import { render, screen } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom';
import { AdminUsersPage } from '../../../pages/admin/AdminUsersPage';
import { useAdminAccess } from '../../../core/auth/useAdminAccess';
import { ReactElement } from 'react';

jest.mock('../../../core/auth/useAdminAccess');

const mockUseAdminAccess = useAdminAccess as jest.MockedFunction<typeof useAdminAccess>;

const renderWithRouter = (component: ReactElement) => {
  return render(<BrowserRouter>{component}</BrowserRouter>);
};

describe('AdminUsersPage', () => {
  it('should show loading state', () => {
    mockUseAdminAccess.mockReturnValue({
      isAdmin: false,
      adminRecord: null,
      loading: true,
      error: null,
    });

    renderWithRouter(<AdminUsersPage />);

    expect(screen.getByRole('progressbar')).toBeInTheDocument();
  });

  it('should redirect non-admin users', () => {
    mockUseAdminAccess.mockReturnValue({
      isAdmin: false,
      adminRecord: null,
      loading: false,
      error: null,
    });

    renderWithRouter(<AdminUsersPage />);

    // Component redirects, so we won't see the page content
    expect(screen.queryByText('User Management')).not.toBeInTheDocument();
  });

  it('should render page for admin users', () => {
    mockUseAdminAccess.mockReturnValue({
      isAdmin: true,
      adminRecord: {
        auth0Sub: 'auth0|123',
        email: 'admin@example.com',
        role: 'admin',
        createdAt: '2024-01-01',
        createdBy: 'system',
        revokedAt: null,
        updatedAt: '2024-01-01',
      },
      loading: false,
      error: null,
    });

    renderWithRouter(<AdminUsersPage />);

    expect(screen.getByText('User Management')).toBeInTheDocument();
    expect(screen.getByText('Admin Users')).toBeInTheDocument();
  });
});
