modules/frontend/Write-Frontend-RN-Navigation.ps1

param(
    [string]$ProjectName,
    [string]$OutputPath,
    [string]$Platform,
    [string[]]$Entities,
    [string]$Language = "tr"
)

. (Join-Path (Split-Path $MyInvocation.MyCommand.Path) "..\core\Utils.ps1")
. (Join-Path (Split-Path $MyInvocation.MyCommand.Path) "..\core\Get-Locale.ps1")
$L = Get-Locale -Language $Language

$MobilePath = Join-Path $OutputPath "$ProjectName-mobile"
$AppPath = Join-Path $MobilePath "app"

if (-not $Global:DryRun) {
    New-Item -ItemType Directory -Path $AppPath -Force | Out-Null
    New-Item -ItemType Directory -Path (Join-Path $AppPath "(tabs)") -Force | Out-Null
    New-Item -ItemType Directory -Path (Join-Path $AppPath "(auth)") -Force | Out-Null
}

$nonUserEntities = $Entities | Where-Object { $_ -ne "User" }

# 0. Write root app/index.tsx (Initial entry route redirecting to (tabs))
$rootIndex = @'
import { Redirect } from 'expo-router';
 
export default function Index() {
  return <Redirect href="/(tabs)" />;
}
'@

Write-CoreFile (Join-Path $AppPath "index.tsx") $rootIndex

# 1. Write root app/_layout.tsx
$rootLayout = @'
import { Stack } from 'expo-router';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { AuthProvider } from '../context/AuthContext';
import { StatusBar } from 'expo-status-bar';
 
const queryClient = new QueryClient();
 
export default function RootLayout() {
  return (
    <QueryClientProvider client={queryClient}>
      <AuthProvider>
        <StatusBar style="light" />
        <Stack screenOptions={{ headerShown: false }}>
          <Stack.Screen name="(tabs)" />
          <Stack.Screen name="(auth)" />
        </Stack>
      </AuthProvider>
    </QueryClientProvider>
  );
}
'@

Write-CoreFile (Join-Path $AppPath "_layout.tsx") $rootLayout

# 2. Write app/(tabs)/_layout.tsx (with dynamic tabs)
$tabsScreens = ""
foreach ($e in $nonUserEntities) {
    $pE = Get-Plural $e
    $tabsScreens += " <Tabs.Screen name=`"$($pE.ToLower())`" options={{ title: `"$pE`", headerShown: true }} />`n"
}

$tabsLayout = @"
import { Tabs, Redirect } from 'expo-router';
import { useAuth } from '../../context/AuthContext';
import { ActivityIndicator, View } from 'react-native';
 
export default function TabsLayout() {
  const { token, loading } = useAuth();
 
  if (loading) {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#121212' }}>
        <ActivityIndicator size="large" color="#3b82f6" />
      </View>
    );
  }
 
  if (!token) {
    return <Redirect href="/(auth)/login" />;
  }
 
  return (
    <Tabs screenOptions={{
      headerStyle: { backgroundColor: '#1e1e1e', borderBottomWidth: 0 },
      headerTintColor: '#ffffff',
      tabBarStyle: { backgroundColor: '#1e1e1e', borderTopWidth: 0, paddingBottom: 5, height: 60 },
      tabBarActiveTintColor: '#3b82f6',
      tabBarInactiveTintColor: '#888888',
      tabBarLabelStyle: { fontSize: 12 },
    }}>
      <Tabs.Screen name="index" options={{ title: 'Dashboard', headerShown: true }} />
$tabsScreens </Tabs>
  );
}
"@

Write-CoreFile (Join-Path $AppPath "(tabs)\_layout.tsx") $tabsLayout

# 3. Write app/(tabs)/index.tsx (Dashboard)
$quickNavItems = ""
foreach ($e in $nonUserEntities) {
    $pE = Get-Plural $e
    $quickNavItems += " <TouchableOpacity style={styles.card} onPress={() => router.push('/$($pE.ToLower())')}>`n <Text style={styles.cardText}>Go to $pE</Text>`n </TouchableOpacity>`n"
}

$dashboard = @"
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
import { useAuth } from '../../context/AuthContext';
import { useRouter } from 'expo-router';
 
export default function DashboardScreen() {
  const { username, logout } = useAuth();
  const router = useRouter();
 
  return (
    <View style={styles.container}>
      <Text style={styles.title}>Welcome, {username}!</Text>
      <Text style={styles.subtitle}>$ProjectName - Mobile Client</Text>
       
      <View style={styles.grid}>
$quickNavItems </View>
 
      <TouchableOpacity style={styles.logoutButton} onPress={logout}>
        <Text style={styles.logoutText}>$($L.uiLogout)</Text>
      </TouchableOpacity>
    </View>
  );
}
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#121212',
    padding: 20,
    justifyContent: 'center',
    alignItems: 'center',
  },
  title: {
    fontSize: 26,
    fontWeight: 'bold',
    color: '#ffffff',
    marginBottom: 8,
  },
  subtitle: {
    fontSize: 16,
    color: '#888888',
    marginBottom: 40,
  },
  grid: {
    width: '100%',
    marginBottom: 40,
  },
  card: {
    backgroundColor: '#1e1e1e',
    padding: 18,
    borderRadius: 8,
    alignItems: 'center',
    marginBottom: 12,
    borderWidth: 1,
    borderColor: '#333333',
  },
  cardText: {
    color: '#3b82f6',
    fontWeight: '600',
    fontSize: 16,
  },
  logoutButton: {
    backgroundColor: '#ef4444',
    paddingVertical: 12,
    paddingHorizontal: 30,
    borderRadius: 8,
    width: '100%',
    alignItems: 'center',
  },
  logoutText: {
    color: '#ffffff',
    fontWeight: 'bold',
    fontSize: 16,
  },
});
"@

Write-CoreFile (Join-Path $AppPath "(tabs)\index.tsx") $dashboard