/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 */

import React from 'react';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import { useSelector } from 'react-redux';
import { NavigationContainer } from '@react-navigation/native';
import AppNavigator from '@/navigation/AppNavigator';
import { store, persistor } from '@/store/store';
import { ThemeProvider } from '@/shared/styles/ThemeProvider';
import LoadingSpinner from '@/shared/components/ui/LoadingSpinner';
import AuthNavigator from '@/modules/auth/navigation/AuthNavigator';
import type { RootState } from '@/store/store';
import IntegrationsNavigator from '@/modules/integrations/navigation/IntegrationsNavigator';
import { StatusBar } from 'react-native';

function AppContent(): React.JSX.Element {
  const isAuthenticated = useSelector((s: RootState) => Boolean(s.auth.token));
  const selectedService = useSelector((s: RootState) => s.integrations.selectedService);
  return (    
        <ThemeProvider>
       <PersistGate loading={<LoadingSpinner />} persistor={persistor}>
       <StatusBar backgroundColor={'#FFFFFF'} barStyle={'dark-content'} />
          {!isAuthenticated ? (
              <NavigationContainer>
           <AuthNavigator />
         </NavigationContainer>
          ) : (
            !selectedService ? (
              <NavigationContainer>
                <IntegrationsNavigator/>
              </NavigationContainer>
            ) : (
              <AppNavigator />
            )
          )}
      </PersistGate>
      </ThemeProvider>

  );
}


export default function App() {
  return (
    <Provider store={store}>
      <AppContent />
    </Provider>
  );
}
