# UClaude Updater — automatic Claude Code patch updater
# Usage: powershell -ExecutionPolicy Bypass -File claude\uclaude_update.ps1 [--check] [--force] [--settings-only]

$ErrorActionPreference = "Continue"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$RepoRoot = Split-Path -Parent $ScriptDir
Set-Location $RepoRoot

# ---- Pull latest FIRST so we always run newest scripts ----

Write-Host "  Updating from remote..." -ForegroundColor Cyan
git fetch --depth 1 origin master 2>$null
git reset --hard origin/master 2>$null
if ($LASTEXITCODE -ne 0) {
    git pull --quiet 2>$null
}

# ---- Helpers ----

function Test-Command($cmd) {
    return [bool](Get-Command $cmd -ErrorAction SilentlyContinue)
}

function Refresh-Path {
    $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" +
                [System.Environment]::GetEnvironmentVariable("Path", "User")
}

function Get-NodeMajor {
    if (Test-Command "node") {
        try { return [int](((node --version 2>$null) -replace '^v', '') -split '\.')[0] }
        catch { return 0 }
    }
    return 0
}

# ---- Check prerequisites ----

# Git
if (-not (Test-Command "git")) {
    Write-Host "  git not found." -ForegroundColor Red
    if (Test-Command "winget") {
        winget install --id Git.Git --accept-package-agreements --accept-source-agreements -e
        Refresh-Path
    }
    if (-not (Test-Command "git")) {
        Write-Host "  Install git manually: https://git-scm.com/download/win" -ForegroundColor Yellow
        exit 1
    }
}

# Python
$hasPython = (Test-Command "python3") -or (Test-Command "python")
if (-not $hasPython) {
    Write-Host "  Python not found." -ForegroundColor Red
    if (Test-Command "winget") {
        winget install --id Python.Python.3.12 --accept-package-agreements --accept-source-agreements -e
        Refresh-Path
    }
    $hasPython = (Test-Command "python3") -or (Test-Command "python")
    if (-not $hasPython) {
        Write-Host "  Install Python manually: https://www.python.org/downloads/" -ForegroundColor Yellow
        exit 1
    }
}

# Node.js v18+ (Claude Code requires v18+, v20/v22/v24 all work)
$MIN_NODE_MAJOR = 18
$nodeMajor = Get-NodeMajor

if ($nodeMajor -ge $MIN_NODE_MAJOR) {
    Write-Host "  Node.js v$nodeMajor.x OK" -ForegroundColor Green
} else {
    if ($nodeMajor -gt 0) {
        Write-Host "  Node.js v$nodeMajor found, need v$MIN_NODE_MAJOR+. Upgrading..." -ForegroundColor Yellow
    } else {
        Write-Host "  Node.js not found. Installing..." -ForegroundColor Yellow
    }

    $installed = $false

    # 1. Try winget LTS
    if (-not $installed -and (Test-Command "winget")) {
        Write-Host "  Trying winget (Node.js LTS)..." -ForegroundColor Yellow
        winget install --id OpenJS.NodeJS.LTS --accept-package-agreements --accept-source-agreements -e 2>$null
        Refresh-Path
        if ((Get-NodeMajor) -ge $MIN_NODE_MAJOR) { $installed = $true }
    }

    # 2. Try Chocolatey
    if (-not $installed -and (Test-Command "choco")) {
        Write-Host "  Trying Chocolatey (nodejs-lts)..." -ForegroundColor Yellow
        choco install nodejs-lts -y 2>$null
        Refresh-Path
        if ((Get-NodeMajor) -ge $MIN_NODE_MAJOR) { $installed = $true }
    }

    # 3. Direct MSI download — Node.js 22 LTS (most compatible)
    if (-not $installed) {
        Write-Host "  Downloading Node.js 22 LTS MSI..." -ForegroundColor Yellow
        try {
            # Try to get latest LTS version dynamically, fallback to known good version
            $latestLts = "v22.14.0"
            try {
                $releases = Invoke-RestMethod -Uri "https://nodejs.org/dist/index.json" -UseBasicParsing -TimeoutSec 10
                $lts = $releases | Where-Object { $_.lts -and $_.version -match "^v22\." } | Select-Object -First 1
                if ($lts) { $latestLts = $lts.version }
            } catch {}
            $msiUrl  = "https://nodejs.org/dist/$latestLts/node-$latestLts-x64.msi"
            $msiFile = Join-Path $env:TEMP "node-lts.msi"
            Invoke-WebRequest -Uri $msiUrl -OutFile $msiFile -UseBasicParsing
            Start-Process msiexec.exe -ArgumentList "/i `"$msiFile`" /quiet /norestart" -Wait
            Refresh-Path
            Remove-Item $msiFile -Force -ErrorAction SilentlyContinue
            if ((Get-NodeMajor) -ge $MIN_NODE_MAJOR) { $installed = $true }
        } catch {
            Write-Host "  Download failed: $_" -ForegroundColor Red
        }
    }

    if (-not $installed) {
        Write-Host "" -ForegroundColor Red
        Write-Host "  Could not install Node.js automatically." -ForegroundColor Red
        Write-Host "  Install manually: https://nodejs.org/en/download/ (v20 LTS)" -ForegroundColor Yellow
        Write-Host "  Then re-run this script." -ForegroundColor Yellow
        exit 1
    }

    Write-Host "  Node.js v$(Get-NodeMajor).x installed OK" -ForegroundColor Green
}

# ---- Check admin, re-launch elevated if needed ----

$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
    [Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $isAdmin) {
    Write-Host "  Re-running as Administrator..." -ForegroundColor Yellow
    Start-Process powershell -ArgumentList "-ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`" $args" -Verb RunAs
    exit
}

# ---- Run updater ----

$pythonCmd = if (Test-Command "python3") { "python3" } else { "python" }
& $pythonCmd claude\uclaude_updater.py @args
