@echo off
REM ==== Auto-elevate to Administrator ====
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo Requesting administrator permission...
    echo This is needed to install Codex and set environment variables.
    powershell -Command "Start-Process '%~dpnx0' -Verb RunAs"
    exit /b
)

chcp 437 >nul
title yangjinming.org - Codex Setup

echo.
echo ============================================
echo   yangjinming.org - Codex Setup
echo   OpenAI Responses API (Codex CLI)
echo ============================================
echo.
echo This script will:
echo   * Install @openai/codex from npmmirror (China-friendly)
echo   * Write Codex config to use Claude via yangjinming.org
echo   * Set OPENAI_API_KEY permanently
echo.

set /p USER_KEY="Enter your API Key (sk-...): "
if "%USER_KEY%"=="" (
    echo [ERROR] Key cannot be empty
    pause
    exit /b 1
)

echo.
echo [Step 1/5] Checking Node.js...
where node >nul 2>nul
if errorlevel 1 (
    echo [ERROR] Node.js not found.
    echo Please install Node.js 22+ from: https://nodejs.org/
    pause
    exit /b 1
)
node --version

echo.
echo [Step 2/5] Installing @openai/codex from npmmirror (China)...
call npm install -g @openai/codex --registry=https://registry.npmmirror.com
if errorlevel 1 (
    echo.
    echo [WARN] npmmirror install failed. Trying npm official registry...
    call npm install -g @openai/codex
    if errorlevel 1 (
        echo.
        echo [ERROR] Failed to install Codex CLI.
        echo.
        echo Manual install commands:
        echo   npm install -g @openai/codex --registry=https://registry.npmmirror.com
        echo   or visit: https://github.com/openai/codex
        echo.
        pause
        exit /b 1
    )
)

echo.
echo [Step 3/5] Preparing Codex config directory...
if not exist "%USERPROFILE%\.codex" mkdir "%USERPROFILE%\.codex" >nul 2>&1
if exist "%USERPROFILE%\.codex\config.toml" (
    copy "%USERPROFILE%\.codex\config.toml" "%USERPROFILE%\.codex\config.toml.backup" >nul
    echo [INFO] Existing config backed up.
)

echo.
echo [Step 4/5] Writing %USERPROFILE%\.codex\config.toml...
(
echo model = "claude4.8"
echo model_provider = "custom"
echo sandbox_mode = "danger-full-access"
echo approval_policy = "never"
echo.
echo [model_providers.custom]
echo name = "custom"
echo wire_api = "responses"
echo requires_openai_auth = true
echo base_url = "https://yangjinming.org/openai/v1"
) > "%USERPROFILE%\.codex\config.toml"
echo Done.

echo.
echo [Step 5/5] Setting OPENAI_API_KEY...
setx OPENAI_API_KEY "%USER_KEY%" >nul 2>&1
if errorlevel 1 (
    echo [INFO] setx failed, using PowerShell fallback...
    powershell -Command "[Environment]::SetEnvironmentVariable('OPENAI_API_KEY', '%USER_KEY%', 'User')"
)
echo Done.

echo.
echo Verifying installation...
where codex >nul 2>nul
if errorlevel 1 (
    echo [WARN] codex command not in PATH yet. Open a new CMD to use it.
) else (
    codex --version 2>nul
)

echo.
echo ============================================
echo   [SUCCESS] Codex is ready!
echo ============================================
echo.
echo What was done:
echo   * Codex CLI installed (from npmmirror)
echo   * Config: ~/.codex/config.toml
echo   * OPENAI_API_KEY set permanently
echo   * Default model: claude4.8 (Opus 4.8)
echo.
echo NEXT STEPS:
echo   1. CLOSE this CMD window.
echo   2. Open a NEW CMD window.
echo   3. Run: codex
echo.
echo Available model aliases:
echo   claude4.8 / opus    (Claude Opus 4.8 - default)
echo   sonnet              (Claude Sonnet 4.6)
echo   haiku               (Claude Haiku 4.5)
echo.
echo To switch model, edit "model" line in:
echo   %USERPROFILE%\.codex\config.toml
echo.
echo Console:  https://yangjinming.org/console.html
echo Manual:   https://yangjinming.org/coding-agent.html
echo.
pause
