modules/solution/Install-Frontend.ps1

param(
    [string]$ProjectName,
    [string]$OrmMode,
    [string]$OutputPath,
    [string]$Platform = "Web",
    [bool]$SkipNpm = $false
)

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

# 1. Determine paths
$WebPath = if ($Platform -match "Web|Both") { Join-Path $OutputPath "$ProjectName-web" } else { $null }
$MobilePath = if ($Platform -match "Mobile|Both") { Join-Path $OutputPath "$ProjectName-mobile" } else { $null }

# 2. Write Web files if Web is selected
if ($WebPath) {
    if (-not $Global:DryRun -and -not (Test-Path $WebPath)) {
        New-Item -ItemType Directory -Path $WebPath -Force | Out-Null
    }
    
    $pkg = @{
        name    = "$($ProjectName.ToLower())-web"
        private = $true
        version = "0.1.0"
        type    = "module"
        scripts = @{
            dev     = "vite"
            build   = "tsc && vite build"
            preview = "vite preview"
        }
        dependencies = @{
            "react"                  = "^18.3.1"
            "react-dom"              = "^18.3.1"
            "react-router-dom"       = "^6.27.0"
            "axios"                  = "^1.7.7"
            "@tanstack/react-query"  = "^5.62.0"
        }
        devDependencies = @{
            "@types/react"         = "^18.3.11"
            "@types/react-dom"     = "^18.3.1"
            "@vitejs/plugin-react" = "^4.3.3"
            "typescript"           = "^5.6.2"
            "vite"                 = "^5.4.10"
        }
    }
    
    $pkgJson = $pkg | ConvertTo-Json -Depth 10
    Write-CoreFile (Join-Path $WebPath "package.json") $pkgJson

    Write-CoreFile (Join-Path $WebPath "vite.config.ts") "import { defineConfig } from 'vite';`nimport react from '@vitejs/plugin-react';`nexport default defineConfig({ plugins: [react()] });"
    Write-CoreFile (Join-Path $WebPath "tsconfig.json") @'
{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": ["./**/*.ts", "./**/*.tsx"],
  "exclude": ["node_modules", "vite.config.ts"],
  "references": [{ "path": "./tsconfig.node.json" }]
}
'@

    Write-CoreFile (Join-Path $WebPath "tsconfig.node.json") @'
{
  "compilerOptions": {
    "composite": true,
    "skipLibCheck": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "allowSyntheticDefaultImports": true
  },
  "include": ["vite.config.ts"]
}
'@

}

# 3. Write Mobile files if Mobile is selected
if ($MobilePath) {
    if (-not $Global:DryRun -and -not (Test-Path $MobilePath)) {
        New-Item -ItemType Directory -Path $MobilePath -Force | Out-Null
    }
    
    $mobilePkg = [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") ($mobilePkg | ConvertTo-Json -Depth 10)
    Write-CoreFile (Join-Path $MobilePath "index.js") "import 'expo-router/entry';"
}

# 4. Trigger npm install inside Web and Mobile folders
if (-not $Global:DryRun -and -not $SkipNpm) {
    $targets = @()
    if ($WebPath) { $targets += $WebPath }
    if ($MobilePath) { $targets += $MobilePath }
    
    foreach ($npmTarget in $targets) {
        Write-Host "Running npm install in: $npmTarget" -ForegroundColor Cyan
        Push-Location $npmTarget
        try {
            Invoke-Cmd "npm" "install --prefer-offline --no-audit --no-fund"
        } catch {
            Write-Warn "npm install failed in $npmTarget - project files are still valid."
            Write-Warn "Run 'npm install' manually in: $npmTarget"
        }
        Pop-Location
    }
}