/**
 * 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';
import Toast from 'react-native-toast-message';

function AppContent(): React.JSX.Element {
  const isAuthenticated = useSelector((s: RootState) => Boolean(s.auth.isAuthenticated));
  const selectedService = useSelector((s: RootState) => s.integrations.selectedService);
  const linking = {
    prefixes: ['centralizedreportingsystem://', 'https://centralizedreportingsystem.com'],
    config: {
      screens: {
        // App tabs (see src/navigation/AppNavigator.tsx)
        Dashboard: {
          path: 'dashboard',
        },
        Profile: {
          path: 'profile',
          screens: {
            Profile: {
              path: ':userId?',
              parse: {
                userId: (userId: string) => (userId ? parseInt(userId, 10) : undefined),
              },
            },
          },
        },
      },
    },
  };
  return (    
        <ThemeProvider>
       <PersistGate loading={<LoadingSpinner />} persistor={persistor}>
       <StatusBar backgroundColor={'#FFFFFF'} barStyle={'dark-content'} />
          {!isAuthenticated ? (
              <NavigationContainer>
           <AuthNavigator />
         </NavigationContainer>
          ) : (
            !selectedService ? (
              <NavigationContainer linking={linking} >
                <IntegrationsNavigator/>
              </NavigationContainer>
            ) : (
              <NavigationContainer linking={linking} >
              <AppNavigator />
              </NavigationContainer>
            )
          )}
          <Toast bottomOffset={20} position='bottom'/>
      </PersistGate>
      </ThemeProvider>

  );
}


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