# Codex CLI — Windows Installer
# Usage: powershell -ExecutionPolicy Bypass -File codex\ucodex_install.ps1
#
# Downloads Codex CLI binary from GitHub, applies config patches.
# Codex is a compiled Rust binary (not npm).

$ErrorActionPreference = "Continue"

Write-Host ""
Write-Host "  +--------------------------------------+" -ForegroundColor Cyan
Write-Host "  |    Codex CLI -- Windows Installer    |" -ForegroundColor Cyan
Write-Host "  +--------------------------------------+" -ForegroundColor Cyan
Write-Host ""

# ---- 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")
}

# ---- Check prerequisites ----

# Python 3.11+ (for tomllib)
$pyCmd = $null
foreach ($candidate in @("python3", "python")) {
    if (Test-Command $candidate) {
        try {
            $pyVer = & $candidate -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')" 2>$null
            $parts = $pyVer -split '\.'
            if ([int]$parts[0] -ge 3 -and [int]$parts[1] -ge 11) {
                $pyCmd = $candidate
                break
            }
        } catch {}
    }
}

if (-not $pyCmd) {
    Write-Host "  Python 3.11+ not found. Installing..." -ForegroundColor Yellow
    if (Test-Command "winget") {
        winget install --id Python.Python.3.12 --accept-package-agreements --accept-source-agreements -e 2>$null
        Refresh-Path
        foreach ($candidate in @("python3", "python")) {
            if (Test-Command $candidate) { $pyCmd = $candidate; break }
        }
    }
    if (-not $pyCmd) {
        Write-Host "  Install Python 3.11+ manually: https://www.python.org/downloads/" -ForegroundColor Red
        exit 1
    }
}
Write-Host "  Python OK ($pyCmd)" -ForegroundColor Green

# curl (usually built into Windows 10+)
if (-not (Test-Command "curl.exe") -and -not (Test-Command "curl")) {
    Write-Host "  curl not found. Using Invoke-WebRequest fallback." -ForegroundColor Yellow
}

# ---- Download Codex binary ----

Write-Host "  Checking latest Codex version..." -ForegroundColor Cyan
$githubApi = "https://api.github.com/repos/openai/codex/releases/latest"

try {
    $release = Invoke-RestMethod -Uri $githubApi -UseBasicParsing -TimeoutSec 15
    $tagName = $release.tag_name
    $latestVersion = ($tagName -replace '^rust-v', '')
    Write-Host "  Latest version: $latestVersion" -ForegroundColor Green
} catch {
    Write-Host "  Could not fetch latest version from GitHub." -ForegroundColor Red
    Write-Host "  Check: https://github.com/openai/codex/releases" -ForegroundColor Yellow
    exit 1
}

# Check if already installed and up to date
$currentVersion = ""
if (Test-Command "codex") {
    try {
        $verOut = codex --version 2>$null
        $currentVersion = [regex]::Match($verOut, '\d+\.\d+\.\d+').Value
    } catch {}
}

if ($currentVersion -eq $latestVersion) {
    Write-Host "  Codex v$currentVersion already installed and up to date" -ForegroundColor Green
} else {
    if ($currentVersion) {
        Write-Host "  Updating: $currentVersion -> $latestVersion" -ForegroundColor Yellow
    } else {
        Write-Host "  Installing Codex v$latestVersion..." -ForegroundColor Cyan
    }

    # Determine architecture
    $arch = if ([System.Environment]::Is64BitOperatingSystem) { "x86_64" } else { "i686" }
    $binarySuffix = "$arch-pc-windows-msvc"
    $downloadUrl = "https://github.com/openai/codex/releases/download/rust-v$latestVersion/codex-$binarySuffix.zip"

    Write-Host "  Downloading: codex-$binarySuffix.zip" -ForegroundColor Cyan
    $tempDir = Join-Path $env:TEMP "codex-install-$(Get-Random)"
    New-Item -ItemType Directory -Force -Path $tempDir | Out-Null
    $zipFile = Join-Path $tempDir "codex.zip"

    try {
        Invoke-WebRequest -Uri $downloadUrl -OutFile $zipFile -UseBasicParsing
    } catch {
        Write-Host "  Download failed: $_" -ForegroundColor Red
        # Try tar.gz fallback
        $downloadUrl = "https://github.com/openai/codex/releases/download/rust-v$latestVersion/codex-$binarySuffix.tar.gz"
        Write-Host "  Trying tar.gz: $downloadUrl" -ForegroundColor Yellow
        $tgzFile = Join-Path $tempDir "codex.tar.gz"
        try {
            Invoke-WebRequest -Uri $downloadUrl -OutFile $tgzFile -UseBasicParsing
            tar -xzf $tgzFile -C $tempDir
        } catch {
            Write-Host "  Download failed. Check https://github.com/openai/codex/releases" -ForegroundColor Red
            Remove-Item -Recurse -Force $tempDir -ErrorAction SilentlyContinue
            exit 1
        }
    }

    # Extract zip if downloaded
    if (Test-Path $zipFile) {
        Expand-Archive -Path $zipFile -DestinationPath $tempDir -Force
    }

    # Find codex.exe
    $codexExe = Get-ChildItem -Path $tempDir -Recurse -Filter "codex.exe" | Select-Object -First 1
    if (-not $codexExe) {
        # Try codex without extension
        $codexExe = Get-ChildItem -Path $tempDir -Recurse -Filter "codex" | Where-Object { -not $_.PSIsContainer } | Select-Object -First 1
    }

    if (-not $codexExe) {
        Write-Host "  codex binary not found in archive" -ForegroundColor Red
        Write-Host "  Contents:" -ForegroundColor Yellow
        Get-ChildItem -Path $tempDir -Recurse | ForEach-Object { Write-Host "    $($_.FullName)" }
        Remove-Item -Recurse -Force $tempDir -ErrorAction SilentlyContinue
        exit 1
    }

    # Install to user-accessible location
    $installDir = "$env:LOCALAPPDATA\Programs\codex"
    New-Item -ItemType Directory -Force -Path $installDir | Out-Null
    $destPath = Join-Path $installDir "codex.exe"

    # Kill running codex processes
    Get-Process -Name "codex" -ErrorAction SilentlyContinue | Stop-Process -Force

    Copy-Item -Path $codexExe.FullName -Destination $destPath -Force
    Write-Host "  Installed: $destPath" -ForegroundColor Green

    # Add to PATH if not already there
    $userPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
    if ($userPath -notlike "*$installDir*") {
        [System.Environment]::SetEnvironmentVariable("Path", "$userPath;$installDir", "User")
        $env:Path = "$env:Path;$installDir"
        Write-Host "  Added to PATH: $installDir" -ForegroundColor Green
    }

    Remove-Item -Recurse -Force $tempDir -ErrorAction SilentlyContinue
    Refresh-Path

    # Verify
    try {
        $newVer = codex --version 2>$null
        Write-Host "  Codex installed: $newVer" -ForegroundColor Green
    } catch {
        Write-Host "  Binary installed but could not verify version" -ForegroundColor Yellow
    }
}

# ---- Download and apply patcher ----

Write-Host "  Downloading patcher..." -ForegroundColor Cyan
$patchDir = Join-Path $env:TEMP "codex-patcher-$(Get-Random)"
New-Item -ItemType Directory -Force -Path $patchDir | Out-Null

$repoRaw = "https://git.sensey24.ru/aibot777/unlimitedcoding/raw/branch/master/codex"
$token = "cadffcb0a6a3be728ac1ff619bb40c86588f6837"
$headers = @{ "Authorization" = "token $token" }

try {
    Invoke-WebRequest -Uri "$repoRaw/codex_patcher.py" -OutFile "$patchDir\codex_patcher.py" -UseBasicParsing -Headers $headers
    Invoke-WebRequest -Uri "$repoRaw/codex_config.json" -OutFile "$patchDir\codex_config.json" -UseBasicParsing -Headers $headers
    Write-Host "  Patcher downloaded" -ForegroundColor Green
} catch {
    # Try without auth
    try {
        Invoke-WebRequest -Uri "$repoRaw/codex_patcher.py" -OutFile "$patchDir\codex_patcher.py" -UseBasicParsing
        Invoke-WebRequest -Uri "$repoRaw/codex_config.json" -OutFile "$patchDir\codex_config.json" -UseBasicParsing
        Write-Host "  Patcher downloaded (no auth)" -ForegroundColor Green
    } catch {
        Write-Host "  Download failed: $_" -ForegroundColor Red
        Write-Host "  Clone repo manually: git clone https://git.sensey24.ru/aibot777/unlimitedcoding.git" -ForegroundColor Yellow
        Remove-Item -Recurse -Force $patchDir -ErrorAction SilentlyContinue
        exit 1
    }
}

Write-Host "  Applying patches..." -ForegroundColor Cyan
& $pyCmd "$patchDir\codex_patcher.py" --apply --config "$patchDir\codex_config.json"
if ($LASTEXITCODE -ne 0) {
    Write-Host "  Patching returned exit code $LASTEXITCODE" -ForegroundColor Yellow
}
Write-Host "  Patches applied" -ForegroundColor Green

Remove-Item -Recurse -Force $patchDir -ErrorAction SilentlyContinue

# ---- Configure environment variables ----

Write-Host "  Setting environment variables..." -ForegroundColor Cyan
[System.Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "ClauderAPI", "User")
[System.Environment]::SetEnvironmentVariable("OPENAI_BASE_URL", "https://ai.37-187-136-86.sslip.io/v1", "User")
$env:OPENAI_API_KEY = "ClauderAPI"
$env:OPENAI_BASE_URL = "https://ai.37-187-136-86.sslip.io/v1"
Write-Host "  Env vars set (OPENAI_API_KEY, OPENAI_BASE_URL)" -ForegroundColor Green

# ---- Verify ----

Write-Host ""
Write-Host "  Verifying..." -ForegroundColor Cyan
Refresh-Path

try {
    $result = & codex exec "Reply with just the number 42" 2>&1 | Out-String
    if ($result -match "42") {
        Write-Host ""
        Write-Host "  Codex CLI installed and patched!" -ForegroundColor Green
        Write-Host ""
        Write-Host "  Usage:"
        Write-Host "    codex                          # interactive mode"
        Write-Host "    codex exec `"Your prompt`"       # single prompt"
        Write-Host ""
        Write-Host "  Models:"
        Write-Host "    gpt-5.4, gpt-5.3-codex-spark"
        Write-Host "    gpt-5.3-codex, gpt-5.2-codex"
    } else {
        Write-Host "  Patches applied but test prompt did not return expected result." -ForegroundColor Yellow
        Write-Host "  Try: codex exec `"Hello`"" -ForegroundColor Yellow
    }
} catch {
    Write-Host "  Could not run test. Try: codex exec `"Hello`"" -ForegroundColor Yellow
}

Write-Host ""
