import { useEffect } from 'react';
import { Redirect } from 'expo-router';

import { AppRouteStack } from '../../src/app-navigation/AppRouteStack';
import { messageManager, useAuthStore } from '../../src/stores';
import { useRegisterPushDevice } from '../../src/hooks';
import { hrefAuthLogin } from '../../src/navigation/hrefs';

export default function AppLayout() {
  const isAuthenticated = useAuthStore((s) => s.isAuthenticated);
  const userID = useAuthStore((s) => s.currentUser?.id);

  useEffect(() => {
    messageManager.initialize();
  }, []);

  useRegisterPushDevice(isAuthenticated, userID);

  if (!isAuthenticated) {
    return <Redirect href={hrefAuthLogin()} />;
  }

  return <AppRouteStack />;
}
