modules/solution/Install-Frontend.ps1

param(
    [string]$ProjectName,
    [string]$OrmMode,
    [string]$OutputPath,
    [string]$FrontendMode,
    [bool]$I18n = $false,
    [string]$Platform = "Web",
    [string]$Architecture = "Layered",
    [string]$MobileTooling = "Expo",
    [bool]$SkipNpm = $false
)

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

# 1. Determine paths
$WebPath = switch ($Platform) {
    "Both"   { Join-Path $OutputPath "apps\web" }
    "Mobile" { $null }
    default  { Join-Path $OutputPath "frontend" }
}

$MobilePath = switch ($Platform) {
    "Both"   { Join-Path $OutputPath "apps\mobile" }
    "Mobile" { Join-Path $OutputPath "mobile" }
    default  { $null }
}

# 2. Write Web files if needed
if ($WebPath) {
    if (-not $Global:DryRun -and -not (Test-Path $WebPath)) {
        New-Item -ItemType Directory -Path $WebPath -Force | Out-Null
    }
    
    $pkg = @{
        name    = "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"
        }
    }
    
    if ($Platform -eq "Both") {
        $pkg.dependencies["shared"] = "*"
    } else {
        if ($I18n -and $FrontendMode -ne "NextJS") {
            $pkg.dependencies["i18next"]                          = "^23.16.4"
            $pkg.dependencies["react-i18next"]                    = "^15.1.1"
            $pkg.dependencies["i18next-browser-languagedetector"] = "^8.0.2"
            $pkg.dependencies["i18next-http-backend"]             = "^2.6.2"
        }
    }

    if ($FrontendMode -eq "NextJS") {
        $pkg.scripts = @{
            dev   = "next dev"
            build = "next build"
            start = "next start"
            lint  = "next lint"
        }
        $pkg.dependencies = @{
            "react"       = "^18"
            "react-dom"   = "^18"
            "next"        = "15.0.1"
            "lucide-react" = "^0.454.0"
        }
        if ($Platform -eq "Both") {
            $pkg.dependencies["shared"] = "*"
        }
    }

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

    if ($FrontendMode -ne "NextJS") {
        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"]
}
'@

    }
}

# 2.5 Write Mobile files if needed
if ($MobilePath) {
    if (-not $Global:DryRun -and -not (Test-Path $MobilePath)) {
        New-Item -ItemType Directory -Path $MobilePath -Force | Out-Null
    }
    
    $mobilePkg = @{
        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") {
        $mobilePkg.dependencies["shared"] = "*"
    }
    
    Write-CoreFile (Join-Path $MobilePath "package.json") ($mobilePkg | ConvertTo-Json -Depth 10)
}

# 3. Write Root package.json for workspaces (Only for Both)
if ($Platform -eq "Both") {
    $rootPkg = @{
        name    = "$($ProjectName.ToLower())-monorepo"
        private = $true
        workspaces = @("apps/*", "packages/*")
    }
    Write-CoreFile (Join-Path $OutputPath "package.json") ($rootPkg | ConvertTo-Json -Depth 10)
}

# 4. Trigger npm install
if (-not $Global:DryRun -and -not $SkipNpm) {
    if ($Platform -eq "Both") {
        Push-Location $OutputPath
        Invoke-Cmd "npm" "install --prefer-offline --no-audit --no-fund"
        Pop-Location
    } elseif ($Platform -eq "Mobile" -and $MobilePath) {
        Push-Location $MobilePath
        Invoke-Cmd "npm" "install --prefer-offline --no-audit --no-fund"
        Pop-Location
    } elseif ($Platform -eq "Web" -and $WebPath) {
        Push-Location $WebPath
        Invoke-Cmd "npm" "install --prefer-offline --no-audit --no-fund"
        Pop-Location
    }
}