@echo off

:: Activate the virtual environment
call C:\Users\$env:USERNAME\Common\env_dedup\Scripts\Activate.ps1

:: Check if the first argument is a directory
if not exist "%~1\" (
    echo Error: '%~1' is not a directory.
    exit /b 1
)

:: Get list of images
setlocal enabledelayedexpansion
set count=0
for %%f in ("%~1\*.jpg" "%~1\*.png") do (
    set /a count+=1
    set images[!count!]=%%~nxf
)

:: Check if we found any images
if %count% equ 0 (
    echo No images found.
    exit /b 1
)

:: Outer loop
for /l %%i in (1,1,%count%) do (
    set outer_image=!images[%%i]!
    
    :: Inner loop (only later images to avoid double-checks)
    set /a j=%%i+1
    for /l %%j in (!j!,1,%count%) do (
        set inner_image=!images[%%j]!
        
        echo Compairing files: !outer_image! TO !inner_image!
        echo.
        python dedup.py "%~1\!outer_image!" "%~1\!inner_image!"
        set exit_code=!errorlevel!
        if !errorlevel! equ 1 (
            echo %~1\!outer_image!>> dups.txt
            goto :break_inner
        )
        if !errorlevel! equ 2 (
            echo %~1\!outer_image!>> alike.txt
            goto :break_inner
        )
        if !errorlevel! equ 5 (
            echo %~1\!outer_image!>> sameGPS.txt
            goto :break_inner
        )
        if !errorlevel! equ 8 (
            echo %~1\!outer_image!>> invalid.txt
            goto :break_inner
        )
    )
    :break_inner
)

endlocal
