@echo off

:: Define the API endpoint and output file names.
set "API_URL=https://api.tarkov.dev/graphql"
set "OUTPUT_FILE=tarkov_prices.json"
set "PAYLOAD_FILE=temp_payload.json"

echo Initializing request...

:: Create a temporary JSON request body containing the GraphQL query.
:: This query retrieves every item's ID, name, short name, 24-hour average price,
:: current lowest price, and all available sell prices along with their sources.
> "%PAYLOAD_FILE%" echo {"query": "{ items { id name shortName avg24hPrice lastLowPrice sellFor { price source } } }"}

echo Requesting the latest market item prices from tarkov.dev...
echo This may take a few seconds. Please wait...

:: Use the built-in curl available in Windows 11 to send the POST request.
curl -s -X POST "%API_URL%" ^
     -H "Content-Type: application/json" ^
     -H "Accept: application/json" ^
     -d @"%PAYLOAD_FILE%" ^
     -o "%OUTPUT_FILE%"

:: Check whether the request completed successfully.
if %ERRORLEVEL% equ 0 (
    echo.
    echo [SUCCESS] Data retrieved successfully! Saved to the current directory as: %OUTPUT_FILE%
) else (
    echo.
    echo [ERROR] Request failed. Please check your network connection or try again later.
)

:: Delete the temporary file.
if exist "%PAYLOAD_FILE%" del "%PAYLOAD_FILE%"

echo.
pause