# Foliko Installer

# Fix execution policy first
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force -ErrorAction SilentlyContinue

Write-Host "Foliko Installer"
Write-Host "================="
Write-Host ""

# Refresh PATH
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")

# ============ Check/Install Node.js ============
Write-Host "Checking Node.js..."
if (Get-Command node -ErrorAction SilentlyContinue) {
    Write-Host "Node.js installed: $(node --version)" -ForegroundColor Green
} else {
    Write-Host "Node.js not found, installing..." -ForegroundColor Yellow

    $nodeUrl = "https://nodejs.org/dist/v20.11.0/node-v20.11.0-x64.msi"
    $nodeInstaller = "$env:TEMP\node-installer.msi"

    Write-Host "Downloading Node.js..." -ForegroundColor Cyan
    Invoke-WebRequest -Uri $nodeUrl -OutFile $nodeInstaller -UseBasicParsing

    Write-Host "Installing Node.js (may require admin)..." -ForegroundColor Cyan
    Start-Process msiexec.exe -ArgumentList "/i", $nodeInstaller, "/quiet", "/norestart" -Wait

    $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
    Start-Sleep -Seconds 3

    if (Get-Command node -ErrorAction SilentlyContinue) {
        Write-Host "Node.js installed: $(node --version)" -ForegroundColor Green
        Remove-Item $nodeInstaller -Force -ErrorAction SilentlyContinue
    } else {
        Write-Host "Node.js installation failed" -ForegroundColor Red
        Write-Host "Download manually: https://nodejs.org/" -ForegroundColor Yellow
    }
}
Write-Host ""

# ============ Check/Install Python ============
Write-Host "Checking Python..."
if (Get-Command python -ErrorAction SilentlyContinue) {
    Write-Host "Python installed: $(python --version)" -ForegroundColor Green
} else {
    Write-Host "Python not found, installing..." -ForegroundColor Yellow

    $pythonUrl = "https://www.python.org/ftp/python/3.12.1/python-3.12.1-amd64.exe"
    $pythonInstaller = "$env:TEMP\python-installer.exe"

    Write-Host "Downloading Python..." -ForegroundColor Cyan
    Invoke-WebRequest -Uri $pythonUrl -OutFile $pythonInstaller -UseBasicParsing

    Write-Host "Installing Python (may require admin)..." -ForegroundColor Cyan
    Start-Process $pythonInstaller -ArgumentList "/quiet", "InstallAllUsers=1", "PrependPath=1", "Include_pip=1" -Wait

    $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
    Start-Sleep -Seconds 3

    if (Get-Command python -ErrorAction SilentlyContinue) {
        Write-Host "Python installed: $(python --version)" -ForegroundColor Green
        Remove-Item $pythonInstaller -Force -ErrorAction SilentlyContinue
    } else {
        Write-Host "Python installation failed" -ForegroundColor Red
        Write-Host "Download manually: https://www.python.org/downloads/" -ForegroundColor Yellow
    }
}
Write-Host ""

# ============ Check/Install uv ============
Write-Host "Checking uv..."
if (Get-Command uv -ErrorAction SilentlyContinue) {
    Write-Host "uv installed: $(uv --version)" -ForegroundColor Green
} else {
    Write-Host "uv not found, installing..." -ForegroundColor Yellow

    try {
        Write-Host "Downloading uv..." -ForegroundColor Cyan
        Invoke-WebRequest -Uri "https://astral.sh/uv/install.ps1" -OutFile "$env:TEMP\install-uv.ps1" -UseBasicParsing
        powershell -ExecutionPolicy Bypass -File "$env:TEMP\install-uv.ps1" -Version "0.4.0" -PowerShell -Admin
        Remove-Item "$env:TEMP\install-uv.ps1" -Force -ErrorAction SilentlyContinue

        $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")

        if (Get-Command uv -ErrorAction SilentlyContinue) {
            Write-Host "uv installed: $(uv --version)" -ForegroundColor Green
        }
    } catch {
        Write-Host "uv installation failed (optional, can be skipped)" -ForegroundColor Yellow
    }
}
Write-Host ""

# ============ Clean npm cache and install Foliko ============
Write-Host "Cleaning npm cache..." -ForegroundColor Cyan
npm cache clean --force 2>$null | Out-Null

Write-Host "Installing Foliko..." -ForegroundColor Cyan
npm install -g foliko --ignore-scripts 2>&1 | Out-Null

# Get npm global bin path
$npmPrefix = npm config get prefix 2>$null
$npmBinPath = $npmPrefix -replace "\\$", ""

# Add to PATH permanently for current user
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath -notlike "*$npmBinPath*") {
    [Environment]::SetEnvironmentVariable("Path", "$userPath;$npmBinPath", "User")
}

# Also add to current session PATH
$env:Path = "$npmBinPath;$env:Path"
Start-Sleep -Seconds 2

# Check if installation succeeded
if (Test-Path "$npmBinPath\foliko.cmd") {
    Write-Host ""
    Write-Host "Installation complete!" -ForegroundColor Green
    Write-Host ""
    Write-Host "Please restart your terminal or run:" -ForegroundColor Cyan
    Write-Host '  $env:Path = "C:\Users\Administrator\AppData\Roaming\npm;' + '$env:Path"' -ForegroundColor White
    Write-Host ""
    Write-Host "Then run: foliko chat" -ForegroundColor Yellow
} else {
    Write-Host ""
    Write-Host "Installation may have failed." -ForegroundColor Yellow
    Write-Host "Run: npm install -g foliko"
}
