import { useAuth } from '@/components/AuthProvider';
import LoginScreen from '@/screens/auth/login/ui/LoginScreens';
import { router } from 'expo-router';
import { useEffect } from 'react';
import { ActivityIndicator, ScrollView, View } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';

export default function Index() {
  const { isAuthenticated, isLoading } = useAuth();

  // Loading spinner
  if (isLoading) {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <ActivityIndicator size="large" color="#3b82f6" />
      </View>
    );
  }

  // Token bo‘lsa dashboard-ga yo‘naltir
  useEffect(() => {
    if (isAuthenticated) {
      router.replace('/(dashboard)');
    }
  }, [isAuthenticated]);

  // Token yo‘q → login screen
  if (!isAuthenticated) {
    return (
      <SafeAreaView style={{ flex: 1, backgroundColor: '#0f172a' }}>
        <ScrollView contentContainerStyle={{ flexGrow: 1 }}>
          <LoginScreen />
        </ScrollView>
      </SafeAreaView>
    );
  }

  return null;
}
