@echo off
setlocal EnableDelayedExpansion

rem HOW TO RUN:
rem 1) Interactive mode: run_archive_migration_report.bat
rem 2) Non-interactive mode: run_archive_migration_report.bat --tenant_aapi_url <url> --aapi_token "<token>" [--report_history_days <days>]
rem Example: run_archive_migration_report.bat --tenant_aapi_url https://example.com --aapi_token "RENJTlZ...PQ==" --report_history_days 7
rem 
rem IMPORTANT: Quote the aapi_token value to preserve trailing = characters in base64-encoded tokens.

set "current_dir=%~dp0"
set "python_script_path=%current_dir%scripts\archive_import_status.py"

rem Check if running in interactive mode (no arguments)
set "args=%*"
if "!args!"=="" goto :interactive_mode

rem Check if only --python_exe is provided (treat as interactive mode)
echo !args! | findstr /I /C:"--tenant_aapi_url" >nul
if errorlevel 1 (
    rem Parse --python_exe if provided before going to interactive mode
    set "python_exe="
    :parse_python_only
    if "%~1"=="" goto :interactive_mode
    if /I "%~1"=="--python_exe" (
        set "python_exe=%~2"
        shift
        shift
    ) else (
        shift
    )
    if not "%~1"=="" goto :parse_python_only
    goto :interactive_mode
)

rem Parse --python_exe parameter if provided
set "python_exe="
:parse_args
if "%~1"=="" goto :detect_python
if /I "%~1"=="--python_exe" (
    set "python_exe=%~2"
    shift
    shift
    goto :parse_args
)
shift
goto :parse_args

:detect_python
rem Detect Python interpreter if not provided via --python_exe
if defined python_exe goto :run_script

if "%EM_HOME%"=="" (
    set "python_exe=python3"
) else (
    set "python_exe=bmcpython"
)

:run_script
rem Non-interactive mode - pass all arguments directly to Python (preserves trailing ==)
rem echo Using Python: !python_exe!
!python_exe! "!python_script_path!" %*
goto :end

:interactive_mode
rem Interactive mode - prompt for inputs
set /p "tenant_aapi_url=Type your SaaS Control-M Automation API endpoint: "
if not defined tenant_aapi_url (
    echo ERROR: Control-M SaaS Automation API Endpoint is required.
    exit /b 1
)

set /p "aapi_token=Type your SaaS Control-M Automation API Token: "
if not defined aapi_token (
    echo ERROR: Control-M Automation API Token is required.
    exit /b 1
)

rem Check if --python_exe was already provided in command line
if not defined python_exe (
    rem Detect Python interpreter for interactive mode
    if not "%EM_HOME%"=="" (
        rem Check if em bmcpython is available
        for /f "tokens=*" %%i in ('em bmcpython --version 2^>^&1') do (
            echo %%i | findstr /i "Python" >nul && set "python_exe=em bmcpython"
        )
    )

    if defined python_exe (
        echo Using em bmcpython as Python interpreter.
    ) else (
        set /p "custom_python_exe=Type the name of your Python interpreter (press ENTER to accept python3 as default): "
        if defined custom_python_exe (
            set "python_exe=!custom_python_exe!"
        ) else (
            set "python_exe=python3"
        )
    )
)

goto :call_interactive

:call_interactive
rem Interactive mode - call Python with quoted parameters (variables now accessible outside if block)
rem echo Using Python: !python_exe!
!python_exe! "!python_script_path!" --tenant_aapi_url "!tenant_aapi_url!" --aapi_token "!aapi_token!"
goto :end

:end
if errorlevel 2 (
    rem Application error - script ran but encountered an error
    exit /b %ERRORLEVEL%
)
if errorlevel 1 (
    echo.
    echo ========================================
    echo ERROR: Script execution failed.
    echo If you see "ModuleNotFoundError: No module named 'requests'",
    echo please run: !python_exe! -m pip install requests
    echo ========================================
    echo.
)
exit /b %ERRORLEVEL%
