import React, { type ReactElement } from 'react';
import { render, type RenderOptions } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom';
import { I18nextProvider } from 'react-i18next';
import i18n from '../i18n'; // Adjust path to your i18n config

/**
 * AuthContext mock for tests.
 * Provides a default authenticated user with admin permissions.
 */
const mockAuthContext = {
  user: {
    id: 'test-user-id',
    email: 'test-admin@smartstack.io',
    name: 'Test Admin',
    role: 'Administrator',
  },
  isAuthenticated: true,
  isLoading: false,
  login: async () => {},
  logout: async () => {},
  hasPermission: () => true,
  refreshPermissions: async () => {},
};

/**
 * Wraps components with all necessary providers for testing.
 * Includes: Router, i18n, Auth context.
 */
function AllProviders({ children }: { children: React.ReactNode }) {
  return (
    <BrowserRouter>
      <I18nextProvider i18n={i18n}>
        {children}
      </I18nextProvider>
    </BrowserRouter>
  );
}

/**
 * Custom render function that wraps components with test providers.
 * Use this instead of @testing-library/react's render.
 *
 * @example
 * import { renderWithProviders, screen } from '@/test/test-utils';
 * renderWithProviders(<MyComponent />);
 * expect(screen.getByText('Hello')).toBeInTheDocument();
 */
function renderWithProviders(
  ui: ReactElement,
  options?: Omit<RenderOptions, 'wrapper'>
) {
  return render(ui, { wrapper: AllProviders, ...options });
}

// Re-export everything from testing-library
export * from '@testing-library/react';
export { renderWithProviders };
export { mockAuthContext };
