import { LOG } from "@logger";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import HomeUserNotLoggedIn from "@screens/HomeUserNotLoggedIn";
import PaymentResultScreen from "@screens/PaymentResultScreen";
import UserLoginScreen from "@screens/UserLoginScreen";
import WaveQrCodePaymentScreen from "@screens/WaveQrCodePaymentScreen";
import { memo } from "react";
import { useSelector } from "react-redux";
import type { RootState } from "@/redux";
import { AppBottomTabsNavigator } from "./AppBottomTabsNavigator";
import type { ImainStackNavigator } from "./types";

const Stack = createNativeStackNavigator<ImainStackNavigator>();

const log = LOG.extend("AppMainStackNavigator");

interface IappMainStackNavigatorProps {
	isAuthenticated: boolean;
}

const AppMainStackNavigator: React.FC<IappMainStackNavigatorProps> = ({ isAuthenticated }) => {
	if (!isAuthenticated) {
		log.info("Navigating to UserLoginScreen");

		return (
			<Stack.Navigator
				initialRouteName="homeUserNotLoggedIn"
				screenOptions={{ headerShown: false }}
			>
				<Stack.Screen name="homeUserNotLoggedIn" component={HomeUserNotLoggedIn} />
				<Stack.Screen name="userLoginScreen" component={UserLoginScreen} />
			</Stack.Navigator>
		);
	}

	log.info("Navigating to AppBottomTabsNavigator");
	return (
		<Stack.Navigator
			initialRouteName="appBottomTabsNavigator"
			screenOptions={{ headerShown: false }}
		>
			{/* <Stack.Screen name="homeUserNotLoggedIn" component={HomeUserNotLoggedIn} />
			<Stack.Screen name="userLoginScreen" component={UserLoginScreen} /> */}
			<Stack.Screen name="appBottomTabsNavigator" component={AppBottomTabsNavigator} />
			<Stack.Screen name="paymentResultScreen" component={PaymentResultScreen} />
			<Stack.Screen name="waveQrCodePaymentScreen" component={WaveQrCodePaymentScreen} />
		</Stack.Navigator>
	);
};

export default memo(AppMainStackNavigator);

export const AppMainStackNavigatorAuthWrapper = () => {
	const isAuthenticated = useSelector((state: RootState) => state.auth.isAuthenticated);
	return <AppMainStackNavigator isAuthenticated={isAuthenticated} />;
};
