@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set COLLECTION="C:\Users\ssalomecespe\Downloads\localhost_allinone.json"
set DATA="C:\Users\ssalomecespe\Downloads\Files.csv"
set ENVIRONMENT="C:\Users\ssalomecespe\Downloads\envload.json"
set REPORT_DIR="C:\Users\ssalomecespe\Downloads\reports"
set LOG_FILE="%REPORT_DIR%\upload_log.txt"
set CONSOLE_LOG_FILE="%REPORT_DIR%\console_output.log"

set ITERATIONS=1
set CONCURRENT_REQUESTS=1

:: Create reports directory if it doesn't exist
if not exist %REPORT_DIR% mkdir %REPORT_DIR%

:: Clear previous log files
if exist %LOG_FILE% del %LOG_FILE%
if exist %CONSOLE_LOG_FILE% del %CONSOLE_LOG_FILE%

echo Running concurrent upload tests...
echo -----------------------------------------
echo [!time!] Test started >> %LOG_FILE%

:: Read CSV file to get file names
set line_num=0
for /f "usebackq tokens=1,2 delims=," %%a in ("%DATA%") do (
    set /a line_num+=1
    if !line_num! gtr 1 (
        set "file_path=%%a"
        set "expected_status=%%b"
        
        :: Extract just the filename from the path
        for %%F in ("!file_path!") do set "filename=%%~nxF"
        
        echo [!time!] Test file: !filename! (Expected status: !expected_status!) >> %LOG_FILE%
        echo [!time!] Will upload: !filename! (Expected status: !expected_status!)
    )
)

echo -----------------------------------------
echo Starting test execution with %CONCURRENT_REQUESTS% concurrent requests...
echo.

:: Run Newman with concurrent instances
for /l %%i in (1,1,%CONCURRENT_REQUESTS%) do (
    start "Newman Instance %%i" cmd /c newman run %COLLECTION% ^
      -e %ENVIRONMENT% ^
      -d %DATA% ^
      --insecure ^
      -n %ITERATIONS% ^
      --timeout-request 60000 ^
      --reporters cli,htmlextra ^
      --reporter-htmlextra-export "%REPORT_DIR%\report_%%i.html" ^
      --reporter-json-export "%REPORT_DIR%\results_%%i.json" ^
      --verbose ^
      --color on ^
      >> "%CONSOLE_LOG_FILE%" 2>&1 ^
      && (
          echo [!time!] Instance %%i completed successfully >> %LOG_FILE%
          echo [!time!] Instance %%i completed successfully
      ) || (
          echo [!time!] Instance %%i failed >> %LOG_FILE%
          echo [!time!] Instance %%i failed
      )
)

echo.
echo All test instances started. Monitor command windows for progress.
echo Final reports will be saved to: %REPORT_DIR%
echo.

:: Wait for all instances to complete
:wait
timeout /t 5 >nul
tasklist /fi "WindowTitle eq Newman Instance*" | find "cmd.exe" >nul
if %errorlevel% equ 0 goto wait

echo All test instances completed.
echo Generating final report...

:: Generate consolidated report with file upload information
(
    echo ===== FILE UPLOAD TEST REPORT =====
    echo Test Run Timestamp: %date% %time%
    echo Collection: %COLLECTION%
    echo Data File: %DATA%
    echo Environment: %ENVIRONMENT%
    echo Iterations: %ITERATIONS%
    echo Concurrent Requests: %CONCURRENT_REQUESTS%
    echo.
    echo ==== FILES TESTED ====
    type %LOG_FILE% | find "Test file:"
    echo.
    echo ==== EXECUTION RESULTS ====
    type %LOG_FILE% | find "Instance"
    echo.
    echo ==== CONSOLE LOGS ====
    echo [Last 20 lines of console output]:
    tail -n 20 "%CONSOLE_LOG_FILE%"
    echo.
    echo ==== SUMMARY ====
    echo Total files tested: %line_num%
    echo Successful instances: 
    type %LOG_FILE% | find /c "completed successfully"
    echo Failed instances:
    type %LOG_FILE% | find /c "failed"
) > "%REPORT_DIR%\final_report.txt"

:: Combine all JSON results
type "%REPORT_DIR%\results_*.json" > "%REPORT_DIR%\consolidated_results.json" 2>nul

echo.
echo ===== TEST COMPLETED =====
echo Detailed upload log: %LOG_FILE%
echo Console output: %CONSOLE_LOG_FILE%
echo Final summary report: %REPORT_DIR%\final_report.txt
echo Individual HTML reports: %REPORT_DIR%\report_*.html
echo Consolidated JSON results: %REPORT_DIR%\consolidated_results.json
echo.

pause