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

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

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

$MobilePath = if ($Platform -eq "Both") { Join-Path $OutputPath "apps\mobile" } else { Join-Path $OutputPath "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
$pkg = @{
    name    = "mobile"
    version = "0.1.0"
    private = $true
    scripts = @{
        start    = "expo start"
        android  = "expo start --android"
        ios      = "expo start --ios"
        web      = "expo start --web"
    }
    dependencies = @{
        "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"
        "@tanstack/react-query"          = "^5.62.0"
    }
    devDependencies = @{
        "@babel/core" = "^7.20.0"
        "@types/react" = "~18.2.45"
        "typescript"   = "~5.3.3"
    }
}

if ($Platform -eq "Both") {
    $pkg.dependencies["shared"] = "*"
}

Write-CoreFile (Join-Path $MobilePath "package.json") ($pkg | ConvertTo-Json -Depth 10)

# 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 (Resolves symlinks in workspaces)
$metro = @'
const { getDefaultConfig } = require('expo/metro-config');
const path = require('path');
 
const projectRoot = __dirname;
const workspaceRoot = path.resolve(projectRoot, '../..');
 
const config = getDefaultConfig(projectRoot);
 
if (config.resolver) {
  config.watchFolders = [workspaceRoot];
  config.resolver.nodeModulesPaths = [
    path.resolve(projectRoot, 'node_modules'),
    path.resolve(workspaceRoot, 'node_modules'),
  ];
  config.resolver.disableHierarchicalLookup = true;
}
 
module.exports = config;
'@

# For Mobile Only, metro config doesn't need to resolve workspaces root, but we can write simple metro config
if ($Platform -ne "Both") {
    $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 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