# txt2tex installer for Windows
# Run in PowerShell as Administrator:
#   Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
#   .\install.ps1

$ErrorActionPreference = "Stop"

function Ok($msg)   { Write-Host "  [OK] $msg" -ForegroundColor Green }
function Info($msg) { Write-Host "  [->] $msg" -ForegroundColor Cyan }
function Warn($msg) { Write-Host "  [!]  $msg" -ForegroundColor Yellow }
function Step($n, $msg) {
    Write-Host ""
    Write-Host "[$n/4] $msg" -ForegroundColor Cyan
}

Write-Host ""
Write-Host "  txt2tex installer - plain text to professional PDF" -ForegroundColor Cyan
Write-Host "  ---------------------------------------------------"
Write-Host ""

# ── Step 1: Rust ──────────────────────────────────────────────
Step 1 "Checking Rust"

if (Get-Command cargo -ErrorAction SilentlyContinue) {
    Ok "Rust already installed: $(rustc --version)"
} else {
    Info "Downloading Rust installer..."
    $installer = "$env:TEMP\rustup-init.exe"
    Invoke-WebRequest "https://win.rustup.rs/x86_64" -OutFile $installer
    Info "Running Rust installer..."
    & $installer -y --no-modify-path
    $cargoBin = "$env:USERPROFILE\.cargo\bin"
    $env:PATH = "$cargoBin;$env:PATH"
    $cur = [System.Environment]::GetEnvironmentVariable("PATH", "User")
    if ($cur -notlike "*$cargoBin*") {
        [System.Environment]::SetEnvironmentVariable("PATH", "$cargoBin;$cur", "User")
    }
    Ok "Rust installed"
}

# ── Step 2: Neovim ────────────────────────────────────────────
Step 2 "Checking Neovim"

if (Get-Command nvim -ErrorAction SilentlyContinue) {
    Ok "Neovim already installed"
} else {
    Info "Downloading Neovim..."
    $nvimUrl  = "https://github.com/neovim/neovim/releases/latest/download/nvim-win64.zip"
    $nvimZip  = "$env:TEMP\nvim-win64.zip"
    $nvimDir  = "$env:LOCALAPPDATA\nvim-win64"

    try {
        Invoke-WebRequest $nvimUrl -OutFile $nvimZip
        Expand-Archive -Path $nvimZip -DestinationPath $nvimDir -Force
        Remove-Item $nvimZip

        $nvimBin = "$nvimDir\nvim-win64\bin"
        $cur = [System.Environment]::GetEnvironmentVariable("PATH", "User")
        if ($cur -notlike "*$nvimBin*") {
            [System.Environment]::SetEnvironmentVariable("PATH", "$cur;$nvimBin", "User")
            $env:PATH = "$env:PATH;$nvimBin"
        }
        [System.Environment]::SetEnvironmentVariable("EDITOR", "nvim", "User")
        $env:EDITOR = "nvim"
        Ok "Neovim installed and EDITOR set to nvim"
    } catch {
        Warn "Could not install Neovim. Install manually: https://neovim.io"
    }
}

# ── Step 3: Tectonic ──────────────────────────────────────────
Step 3 "Checking Tectonic"

if (Get-Command tectonic -ErrorAction SilentlyContinue) {
    Ok "Tectonic already installed"
} else {
    Info "Downloading Tectonic binary..."
    $version    = "0.15.0"
    $url        = "https://github.com/tectonic-typesetting/tectonic/releases/download/tectonic%40$version/tectonic-$version-x86_64-pc-windows-msvc.zip"
    $zip        = "$env:TEMP\tectonic.zip"
    $installDir = "$env:USERPROFILE\.cargo\bin"

    New-Item -ItemType Directory -Force -Path $installDir | Out-Null

    try {
        Invoke-WebRequest $url -OutFile $zip
        Expand-Archive -Path $zip -DestinationPath $installDir -Force
        Remove-Item $zip
        Ok "Tectonic installed to $installDir"
    } catch {
        Warn "Binary download failed. Installing via cargo (takes ~5 minutes)..."
        & cargo install tectonic
        Ok "Tectonic installed via cargo"
    }
}

# ── Step 4: txt2tex ───────────────────────────────────────────
Step 4 "Installing txt2tex"

Info "Compiling and installing..."
& cargo install --path .
Ok "txt2tex installed"

# ── Done ──────────────────────────────────────────────────────
Write-Host ""
Write-Host "  ---------------------------------------------------"
Write-Host ""
Write-Host "  Installation complete!" -ForegroundColor Green
Write-Host ""
Write-Host "  Commands:" -ForegroundColor White
Write-Host ""
Write-Host "  txt2tex          " -NoNewline -ForegroundColor Cyan; Write-Host "edit resume, generate PDF"
Write-Host "  txt2tex new      " -NoNewline -ForegroundColor Cyan; Write-Host "create fresh resume.txt"
Write-Host "  txt2tex analyze  " -NoNewline -ForegroundColor Cyan; Write-Host "check resume for weak spots"
Write-Host "  txt2tex --no-edit" -NoNewline -ForegroundColor Cyan; Write-Host "skip editor, generate PDF"
Write-Host "  txt2tex --help   " -NoNewline -ForegroundColor Cyan; Write-Host "show all options"
Write-Host ""
Write-Host "  Restart your terminal for changes to take effect." -ForegroundColor Yellow
Write-Host ""
