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

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

. (Join-Path (Split-Path $MyInvocation.MyCommand.Path) "..\core\Utils.ps1")

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

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

# 1. Write package.json for mobile (standalone)
$pkg = [ordered]@{
    name    = "$($ProjectName.ToLower())-mobile"
    version = "0.1.0"
    main    = "expo-router/entry"
    private = $true
    scripts = [ordered]@{
        start    = "expo start"
        android  = "expo start --android"
        ios      = "expo start --ios"
        web      = "expo start --web"
    }
    dependencies = [ordered]@{
        "expo"                           = "~51.0.0"
        "expo-router"                    = "~3.5.23"
        "expo-status-bar"                = "~1.12.1"
        "react"                          = "18.2.0"
        "react-dom"                      = "18.2.0"
        "react-native"                   = "0.74.5"
        "react-native-safe-area-context" = "4.10.5"
        "react-native-screens"           = "3.31.1"
        "react-native-web"               = "~0.19.10"
        "expo-secure-store"              = "~13.0.2"
        "expo-linking"                   = "~6.3.1"
        "expo-constants"                 = "~16.0.2"
        "@tanstack/react-query"          = "^5.62.0"
    }
    devDependencies = [ordered]@{
        "@babel/core" = "^7.20.0"
        "@types/react" = "~18.2.45"
        "typescript"   = "~5.3.3"
    }
    overrides = [ordered]@{
        "@babel/compat-data" = "7.25.2"
        "expo-linking"       = "~6.3.1"
        "expo-constants"     = "~16.0.2"
    }
}

Write-CoreFile (Join-Path $MobilePath "package.json") ($pkg | ConvertTo-Json -Depth 10)
Write-CoreFile (Join-Path $MobilePath "index.js") "import 'expo-router/entry';"

# 2. Write app.json (Expo Config)
$appJson = @{
    expo = @{
        name               = $ProjectName
        slug               = $ProjectName.ToLower()
        version            = "1.0.0"
        orientation        = "portrait"
        icon               = "./assets/icon.png"
        userInterfaceStyle = "dark"
        splash             = @{
            image           = "./assets/splash.png"
            resizeMode      = "contain"
            backgroundColor = "#121212"
        }
        ios                = @{
            supportsTablet = $true
        }
        android            = @{
            adaptiveIcon = @{
                foregroundImage = "./assets/adaptive-icon.png"
                backgroundColor = "#121212"
            }
        }
        web                = @{
            favicon = "./assets/favicon.png"
        }
        plugins            = @("expo-router")
        scheme             = $ProjectName.ToLower()
    }
}
Write-CoreFile (Join-Path $MobilePath "app.json") ($appJson | ConvertTo-Json -Depth 10)

# 3. Write tsconfig.json
$tsconfig = @'
{
  "extends": "expo/tsconfig.base",
  "compilerOptions": {
    "strict": true
  }
}
'@

Write-CoreFile (Join-Path $MobilePath "tsconfig.json") $tsconfig

# 4. Write babel.config.js
$babel = @'
module.exports = function (api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
  };
};
'@

Write-CoreFile (Join-Path $MobilePath "babel.config.js") $babel

# 5. Write metro.config.js (standalone)
$metro = @'
const { getDefaultConfig } = require('expo/metro-config');
const config = getDefaultConfig(__dirname);
module.exports = config;
'@

Write-CoreFile (Join-Path $MobilePath "metro.config.js") $metro

# 6. Write .env (Local API endpoint)
Write-CoreFile (Join-Path $MobilePath ".env") "EXPO_PUBLIC_API_URL=http://localhost:8080/api"

# 7. Write types/index.ts directly inside the mobile project
$typesDir = Join-Path $MobilePath "types"
if (-not $Global:DryRun) {
    New-Item -ItemType Directory -Path $typesDir -Force | Out-Null
}
$typesContent = ""
foreach ($e in $Entities) {
    if ($e -eq "User") {
        $typesContent += "export interface User {`n id: number;`n username: string;`n role: string;`n isActive: boolean;`n createdAt: string;`n}`n`n"
    } else {
        $extra = if ($e -eq "Product") { "`n price: number;" } else { "" }
        $typesContent += "export interface $e {`n id: number;`n name: string;$extra`n createdAt: string;`n updatedAt: string;`n}`n`n"
    }
}
Write-CoreFile (Join-Path $typesDir "index.ts") $typesContent

# 8. Write solid color placeholder images for Expo (Offline and standalone)
function Write-PlaceholderImage($path, $width, $height) {
    if ($Global:DryRun) { return }
    try {
        Add-Type -AssemblyName System.Drawing
        $bmp = New-Object System.Drawing.Bitmap($width, $height)
        $g = [System.Drawing.Graphics]::FromImage($bmp)
        $color = [System.Drawing.Color]::FromArgb(255, 18, 18, 18) # Sleek Dark Mode gray
        $brush = New-Object System.Drawing.SolidBrush($color)
        $g.FillRectangle($brush, 0, 0, $width, $height)
        $bmp.Save($path, [System.Drawing.Imaging.ImageFormat]::Png)
        $bmp.Dispose()
        $g.Dispose()
    } catch {
        # Fallback - writes a 0-byte file if drawing fails (unlikely, but safe)
        New-Item -ItemType File -Path $path -Force | Out-Null
    }
}

Write-PlaceholderImage (Join-Path $MobilePath "assets\icon.png") 1024 1024
Write-PlaceholderImage (Join-Path $MobilePath "assets\splash.png") 1242 2436
Write-PlaceholderImage (Join-Path $MobilePath "assets\adaptive-icon.png") 1024 1024
Write-PlaceholderImage (Join-Path $MobilePath "assets\favicon.png") 48 48