import { Stack } from 'expo-router'
import { AdminAuthProvider, useAdminAuth } from '../../src/admin/components/AdminAuthProvider'
import { View, ActivityIndicator } from 'react-native'
import { Redirect } from 'expo-router'

function AdminLayoutContent({ children }: { children: React.ReactNode }) {
  const { isAuthenticated, isAdmin, isLoading } = useAdminAuth()

  if (isLoading) {
    return (
      <View style={{ flex: 1, backgroundColor: '#000', justifyContent: 'center', alignItems: 'center' }}>
        <ActivityIndicator size="large" color="#FF6B35" />
      </View>
    )
  }

  if (!isAuthenticated || !isAdmin) {
    return <Redirect href="/admin/login" />
  }

  return (
    <>
      <Stack.Screen options={{ headerShown: false }} />
      {children}
    </>
  )
}

export default function AdminLayout({ children }: { children: React.ReactNode }) {
  return (
    <AdminAuthProvider>
      <AdminLayoutContent>{children}</AdminLayoutContent>
    </AdminAuthProvider>
  )
}
