import { I18nextProvider } from 'react-i18next';
import { Provider } from 'react-redux';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { AuthBootstrap, AuthConfigProvider } from '@ema-platform/auth';
import type { ReactNode } from 'react';
import { store } from '../store';
import { i18n } from '../i18n/config';
import { MantineThemeProvider } from './MantineThemeProvider';
import { ErrorBoundary } from '../components/ErrorBoundary';

const queryClient = new QueryClient({
  defaultOptions: { queries: { retry: 1, staleTime: 1000 * 60 * 5 } },
});

export function AppProviders({ children }: { children: ReactNode }) {
  return (
    <ErrorBoundary>
      <Provider store={store}>
        <QueryClientProvider client={queryClient}>
          <AuthConfigProvider
            value={{
              appName: 'Portal',
              storagePrefix: 'ema-portal',
              loginRedirectPath: '/dashboard',
              enableSignup: true,
              enableForgotPassword: true,
            }}
          >
            <I18nextProvider i18n={i18n}>
              <MantineThemeProvider>
                <AuthBootstrap>{children}</AuthBootstrap>
              </MantineThemeProvider>
            </I18nextProvider>
          </AuthConfigProvider>
        </QueryClientProvider>
      </Provider>
    </ErrorBoundary>
  );
}
