import './global.css';
import { Stack } from 'expo-router/stack';
import { StatusBar } from 'expo-status-bar';
import AuthProvider, { useAuth } from '../context/AuthProvider';
// import { EXPO_PUBLIC_NODE_ENV } from '@env';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { useEffect, useState } from 'react';
import { checkVersion } from '../component/checkVersion';
import { authenticationService } from '../service/authService';
import { usePushNotifications } from './hooks/usePushNotifications';
import Constants from 'expo-constants';
import { Alert, AppState } from 'react-native';
import * as Updates from 'expo-updates';
import { I18nextProvider } from 'react-i18next';
import { View, Text } from 'react-native';
import i18n from '../i18n';
import { useTranslation } from '../util/hooks/useTranslation';
export default function RootLayout() {
    const NODE_ENV = process.env.EXPO_PUBLIC_NODE_ENV;

    const [isLoading, setIsLoading] = useState(true);
    const { user } = useAuth();
    const { expoPushToken, notification } = usePushNotifications();
    const data = JSON.stringify(notification, undefined, 2);

    useEffect(() => {
        const fetchVersion = async () => {
            const response = await authenticationService.getVersion();
            checkVersion(response);
        };

        // Initial version check
        fetchVersion();

        // Set up AppState listener
        const subscription = AppState.addEventListener('change', async (nextAppState) => {
            if (nextAppState === 'active') {
                fetchVersion();
            }
            const update = await Updates.checkForUpdateAsync();
            if (update.isAvailable) {
                await Updates.fetchUpdateAsync();
                await Updates.reloadAsync(); // Reload the app to apply the update
            }
        });

        // Cleanup subscription on unmount
        return () => {
            subscription.remove();
        };
    }, []);

    return (
        <GestureHandlerRootView style={{ flex: 1 }}>
            <AuthProvider>
              <I18nextProvider i18n={i18n}>
                <Stack>
                    <Stack.Screen name="(auth)/(tabs)" options={{ headerShown: false }} />
                    <Stack.Screen name="(public)/login" options={{ headerShown: false }} />
                    <Stack.Screen name="(public)/registerChooseVehiclesOne" options={{ headerShown: false }} />
                    <Stack.Screen name="(public)/registerChooseVehiclesTwo" options={{ headerShown: false }} />
                    <Stack.Screen name="(public)/userTermsPage" options={{ headerShown: false }} />
                    {/* Testing Purpose */}
                    {NODE_ENV == 'development' ? (
                        <Stack.Screen
                            name="(public)/test"
                            options={{
                                headerShown: false,
                                title: 'Test Component Page'
                            }}
                        />
                    ) : (
                        <></>
                    )}
                </Stack>
              </I18nextProvider>
            </AuthProvider>
            <StatusBar style="dark" backgroundColor="#fff" />
        </GestureHandlerRootView>
    );
}
