import React from 'react'
import { Redirect } from 'expo-router';
import { useAuthStore } from '@/stores/auth';

export default function Index() {
  const { isLoggedIn, token } = useAuthStore();

  // 等待持久化完成，避免未重建前误判到登录页
  const [hydrated, setHydrated] = React.useState(useAuthStore.persist.hasHydrated());
  React.useEffect(() => {
    const unsub = useAuthStore.persist.onFinishHydration(() => {
      setHydrated(true);
    });
    return unsub;
  }, []);

  if (!hydrated) {
    // 可按需展示自定义闪屏，这里简单返回 null，防止提前重定向
    return null;
  }
  
  const redirectPath = isLoggedIn && token ? "/(tabs)/clockIn" : "/login";
  return <Redirect href={redirectPath} />;
}
