@echo off
setlocal EnableExtensions
cd /d "%~dp0"

title Krea 2 Image ZIP Batch Creator

echo ============================================================
echo KREA 2 IMAGE ZIP BATCH CREATOR
echo ============================================================
echo.
echo Place this BAT file inside the folder containing your images.
echo.
echo This tool will automatically create ZIP archives containing:
echo   - Up to 30 images per ZIP file
echo.
echo Your original images will not be changed.
echo.

set "PS1=%TEMP%\krea2_zip_batch_creator_%RANDOM%%RANDOM%.ps1"

> "%PS1%" (
    echo param^(
    echo     [Parameter^(Mandatory = $true^)]
    echo     [string]$SourceFolder
    echo ^)
    echo.
    echo $source = [System.IO.Path]::GetFullPath^($SourceFolder^)
    echo $groupSize = 30
    echo $output = Join-Path $source "KREA2_ZIP_BATCHES_30"
    echo $tempRoot = Join-Path $env:TEMP ^("krea2_zip_staging_" + [guid]::NewGuid^(^).ToString^("N"^)^)
    echo.
    echo New-Item -ItemType Directory -Force -Path $output ^| Out-Null
    echo New-Item -ItemType Directory -Force -Path $tempRoot ^| Out-Null
    echo.
    echo $imageExtensions = @^(".webp", ".png", ".jpg", ".jpeg", ".bmp"^)
    echo.
    echo $images = @^(Get-ChildItem -LiteralPath $source -File ^| Where-Object {
    echo     $imageExtensions -contains $_.Extension.ToLowerInvariant^(^)
    echo } ^| Sort-Object Name^)
    echo.
    echo if ^($images.Count -eq 0^) {
    echo     Write-Host ""
    echo     Write-Host "ERROR: No supported image files were found in:"
    echo     Write-Host $source
    echo     Write-Host ""
    echo     Write-Host "Supported formats: PNG, JPG, JPEG, WEBP, BMP"
    echo     Remove-Item -LiteralPath $tempRoot -Recurse -Force -ErrorAction SilentlyContinue
    echo     exit 1
    echo }
    echo.
    echo Get-ChildItem -LiteralPath $output -Filter "KREA2_BATCH_*.zip" -File -ErrorAction SilentlyContinue ^| Remove-Item -Force
    echo.
    echo $batchCount = [math]::Ceiling^($images.Count / $groupSize^)
    echo.
    echo Write-Host ""
    echo Write-Host "Source folder: $source"
    echo Write-Host "Images found: $^($images.Count^)"
    echo Write-Host "Images per ZIP: $groupSize"
    echo Write-Host "ZIP archives to create: $batchCount"
    echo Write-Host "Output folder: $output"
    echo Write-Host ""
    echo.
    echo for ^($batchIndex = 0; $batchIndex -lt $batchCount; $batchIndex++^) {
    echo     $start = $batchIndex * $groupSize
    echo     $end = [math]::Min^($start + $groupSize - 1, $images.Count - 1^)
    echo     $batchNumber = $batchIndex + 1
    echo     $batchName = "KREA2_BATCH_{0:D3}" -f $batchNumber
    echo     $staging = Join-Path $tempRoot $batchName
    echo     $zipPath = Join-Path $output ^($batchName + ".zip"^)
    echo.
    echo     New-Item -ItemType Directory -Force -Path $staging ^| Out-Null
    echo.
    echo     for ^($i = $start; $i -le $end; $i++^) {
    echo         Copy-Item -LiteralPath $images[$i].FullName -Destination $staging -Force
    echo     }
    echo.
    echo     if ^(Test-Path -LiteralPath $zipPath^) {
    echo         Remove-Item -LiteralPath $zipPath -Force
    echo     }
    echo.
    echo     Compress-Archive -Path ^(Join-Path $staging "*"^) -DestinationPath $zipPath -CompressionLevel Optimal
    echo.
    echo     $imagesInBatch = $end - $start + 1
    echo     Write-Host ^("[{0}/{1}] Created: {2} - {3} images" -f $batchNumber, $batchCount, ^(Split-Path $zipPath -Leaf^), $imagesInBatch^)
    echo }
    echo.
    echo Remove-Item -LiteralPath $tempRoot -Recurse -Force -ErrorAction SilentlyContinue
    echo.
    echo Write-Host ""
    echo Write-Host "============================================================"
    echo Write-Host "FINISHED"
    echo Write-Host "============================================================"
    echo Write-Host "Created $batchCount ZIP archives from $^($images.Count^) images."
    echo Write-Host "Folder:"
    echo Write-Host $output
    echo Write-Host ""
    echo Start-Process explorer.exe $output
)

powershell -NoProfile -ExecutionPolicy Bypass -File "%PS1%" -SourceFolder "%~dp0."
set "EXITCODE=%ERRORLEVEL%"

del /q "%PS1%" >nul 2>nul

echo.
if not "%EXITCODE%"=="0" (
    echo The ZIP creator ended with an error.
)

pause
exit /b %EXITCODE%
