modules/solution/Install-Frontend.ps1

param(
    [string]$ProjectName,
    [string]$OrmMode,
    [string]$OutputPath,
    [string]$FrontendMode,
    [bool]$I18n = $false
)

. (Join-Path (Split-Path $MyInvocation.MyCommand.Path) "..\core\Utils.ps1")
$frontendPath = Join-Path $OutputPath "frontend"

if (-not $Global:DryRun) {
    if (-not (Test-Path $frontendPath)) { New-Item -ItemType Directory -Path $frontendPath -Force | Out-Null }
}

# --- package.json (Direct Write) ---
$pkg = @{
    name    = $ProjectName.ToLower()
    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 ($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"
    }
}

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

if ($FrontendMode -ne "NextJS") {
    # vite config
    Write-CoreFile (Join-Path $frontendPath "vite.config.ts") "import { defineConfig } from 'vite';`nimport react from '@vitejs/plugin-react';`nexport default defineConfig({ plugins: [react()] });"
    Write-CoreFile (Join-Path $frontendPath "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 $frontendPath "tsconfig.node.json") @'
{
  "compilerOptions": {
    "composite": true,
    "skipLibCheck": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "allowSyntheticDefaultImports": true
  },
  "include": ["vite.config.ts"]
}
'@

}

# --- Install ---
if (-not $Global:DryRun) { Push-Location $frontendPath }
Invoke-Cmd "npm" "install --prefer-offline --no-audit --no-fund"
if (-not $Global:DryRun) { Pop-Location }