import { useEffect } from 'react';
import { Stack } from 'expo-router';
import { StatusBar } from 'expo-status-bar';
import { useFrameworkReady } from '@/hooks/useFrameworkReady';
import { useFonts } from 'expo-font';
import {
  Inter_400Regular,
  Inter_500Medium,
  Inter_600SemiBold,
  Inter_700Bold,
  Inter_800ExtraBold,
  Inter_900Black,
} from '@expo-google-fonts/inter';
import {
  JetBrainsMono_400Regular,
  JetBrainsMono_500Medium,
  JetBrainsMono_700Bold,
  JetBrainsMono_800ExtraBold,
} from '@expo-google-fonts/jetbrains-mono';
import * as SplashScreen from 'expo-splash-screen';
import { ProductAvailabilityGate } from '@/components/ProductAvailabilityGate';
import { createMobilePlatformSdk, mobileRuntime } from '@/lib/runtime';
import { AuthGate } from '@/components/auth/AuthGate';
import { MobileAuthProvider } from '@/providers/MobileAuthProvider';
import { TradingDataProvider } from '@/providers/TradingDataProvider';

SplashScreen.preventAutoHideAsync();

const mobilePlatformSdk = createMobilePlatformSdk();
console.info('[mobile] platform bootstrap', {
  productId: mobileRuntime.productId,
  tradingApiUrl: mobileRuntime.tradingApiUrl,
  platformApiUrl: mobileRuntime.platformApiUrl,
  sdkReady: Boolean(mobilePlatformSdk),
});

export default function RootLayout() {
  useFrameworkReady();

  const [fontsLoaded, fontError] = useFonts({
    'Inter-Regular': Inter_400Regular,
    'Inter-Medium': Inter_500Medium,
    'Inter-SemiBold': Inter_600SemiBold,
    'Inter-Bold': Inter_700Bold,
    'Inter-ExtraBold': Inter_800ExtraBold,
    'Inter-Black': Inter_900Black,
    'JetBrainsMono-Regular': JetBrainsMono_400Regular,
    'JetBrainsMono-Medium': JetBrainsMono_500Medium,
    'JetBrainsMono-Bold': JetBrainsMono_700Bold,
    'JetBrainsMono-ExtraBold': JetBrainsMono_800ExtraBold,
  });

  useEffect(() => {
    if (fontsLoaded || fontError) {
      SplashScreen.hideAsync();
    }
  }, [fontsLoaded, fontError]);

  if (!fontsLoaded && !fontError) {
    return null;
  }

  return (
    <ProductAvailabilityGate>
      <MobileAuthProvider>
        <AuthGate>
          <TradingDataProvider>
            <Stack screenOptions={{ headerShown: false }}>
              <Stack.Screen name="(tabs)" />
              <Stack.Screen name="marketplace" options={{ presentation: 'modal' }} />
              <Stack.Screen name="chat" options={{ presentation: 'transparentModal', animation: 'fade' }} />
              <Stack.Screen name="+not-found" />
            </Stack>
            <StatusBar style="light" />
          </TradingDataProvider>
        </AuthGate>
      </MobileAuthProvider>
    </ProductAvailabilityGate>
  );
}
