@echo off
echo.
echo  ===========================
echo   Magic Worlds - Installer
echo  ===========================
echo.

set INSTALL_DIR=%USERPROFILE%\MagicWorlds
set ZIP_URL=https://magic-worlds-testing.s3.amazonaws.com/magic_worlds_latest.zip
set VERSION_URL=https://magic-worlds-testing.s3.amazonaws.com/latest.json
set TEMP_ZIP=%TEMP%\MagicWorlds_download.zip

echo Downloading Magic Worlds...
powershell -Command "Invoke-WebRequest -Uri '%ZIP_URL%' -OutFile '%TEMP_ZIP%'"
if not exist "%TEMP_ZIP%" goto FAIL

echo Extracting...
if exist "%INSTALL_DIR%" rmdir /s /q "%INSTALL_DIR%"
powershell -Command "Expand-Archive -Path '%TEMP_ZIP%' -DestinationPath '%USERPROFILE%' -Force"

if not exist "%INSTALL_DIR%\MagicWorlds.exe" goto FAIL

del "%TEMP_ZIP%" 2>nul

powershell -Command "(Invoke-WebRequest -Uri '%VERSION_URL%').Content | ConvertFrom-Json | Select-Object -ExpandProperty version | Out-File -FilePath '%INSTALL_DIR%\installed_version.txt' -Encoding ascii -NoNewline"

powershell -Command "Invoke-WebRequest -Uri '%VERSION_URL%' -OutFile '%INSTALL_DIR%\version.json'" 2>nul

echo Creating desktop shortcut...
powershell -Command "$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut([Environment]::GetFolderPath('Desktop') + '\Magic Worlds.lnk'); $s.TargetPath = '%INSTALL_DIR%\MagicWorlds.exe'; $s.WorkingDirectory = '%INSTALL_DIR%'; $s.Description = 'Magic Worlds PC'; $s.Save()"

echo Creating Start Menu shortcut...
powershell -Command "$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut([Environment]::GetFolderPath('StartMenu') + '\Programs\Magic Worlds.lnk'); $s.TargetPath = '%INSTALL_DIR%\MagicWorlds.exe'; $s.WorkingDirectory = '%INSTALL_DIR%'; $s.Description = 'Magic Worlds PC'; $s.Save()"

echo.
echo  ===========================
echo   Installation Complete!
echo  ===========================
echo.
echo  Installed to: %INSTALL_DIR%
echo  Desktop shortcut created.
echo  Start Menu shortcut created.
echo.
echo  Launch Magic Worlds from your Desktop!
echo.
pause
goto END

:FAIL
echo.
echo Download or extraction failed. Check your internet connection.
echo.
pause

:END
