@echo off
setlocal enabledelayedexpansion

:: Excel Filter Tool - Environment Check
:: This script checks if Python is available and provides appropriate guidance

echo Excel Filter Tool - Environment Check
call :log_message "Checking system environment..."

:: Check if Python is available
call :find_python
if "!PYTHON_FOUND!" == "1" (
    call :log_message "Python found: !PYTHON_PATH!"
    call :run_python_check
) else (
    call :log_message "Python not found on this system"
    call :show_python_instructions
)

endlocal
goto :eof

:find_python
set PYTHON_FOUND=0
set PYTHON_PATH=

:: Method 1: Check if python is in PATH
call :log_message "Checking PATH for Python..."
where python >nul 2>&1
if !errorlevel! equ 0 (
    set PYTHON_FOUND=1
    set PYTHON_PATH=python
    goto :eof
)

:: Method 2: Check common Python installation locations
call :log_message "Checking common Python installation locations..."
for %%P in (
    "%ProgramFiles%\Python*\python.exe",
    "%ProgramFiles(x86)%\Python*\python.exe", 
    "%LocalAppData%\Programs\Python*\python.exe",
    "%UserProfile%\AppData\Local\Microsoft\WindowsApps\python.exe",
    "%UserProfile%\AppData\Local\Programs\Python*\python.exe"
) do (
    if exist %%P (
        set PYTHON_FOUND=1
        set PYTHON_PATH=%%P
        call :log_message "Found Python at: %%P"
        goto :eof
    )
)

goto :eof

:run_python_check
call :log_message "Running Python environment check..."

:: Run the Python check script
"!PYTHON_PATH!" "%~dp0check_python.py"
if !errorlevel! equ 0 (
    call :log_message "Python environment check completed successfully"
) else (
    call :log_message "Python environment check failed"
    call :show_python_instructions
)

goto :eof

:show_python_instructions
call :log_message "========================================"
call :log_message "Python is required to run Excel Filter Tool"
call :log_message "========================================"
call :log_message ""
call :log_message "Please install Python 3.9 or later:"
call :log_message "1. Download Python from: https://www.python.org/downloads/"
call :log_message "2. Run the installer"
call :log_message "3. Make sure to check 'Add Python to PATH' during installation"
call :log_message "4. Restart your computer after installation"
call :log_message "5. Run Excel Filter Tool again"
call :log_message ""
call :log_message "For technical users:"
call :log_message "- Recommended version: Python 3.9+"
call :log_message "- Architecture: 64-bit (recommended)"
call :log_message "- Required packages: pandas, openpyxl, customtkinter"
call :log_message ""

:: Create a help file with instructions
set HELP_FILE=%TEMP%\ExcelFilter_Python_Help.txt
echo Excel Filter Tool - Python Installation Instructions > "!HELP_FILE!"
echo. >> "!HELP_FILE!"
echo Python is required to run the Excel Filter Tool. >> "!HELP_FILE!"
echo. >> "!HELP_FILE!"
echo Installation Steps: >> "!HELP_FILE!"
echo 1. Download Python from: https://www.python.org/downloads/ >> "!HELP_FILE!"
echo 2. Run the Python installer >> "!HELP_FILE!"
echo 3. Check "Add Python to PATH" during installation >> "!HELP_FILE!"
echo 4. Complete the installation >> "!HELP_FILE!"
echo 5. Restart your computer >> "!HELP_FILE!"
echo 6. Run Excel Filter Tool again >> "!HELP_FILE!"
echo. >> "!HELP_FILE!"
echo Note: Excel Filter Tool requires Python 3.9 or later. >> "!HELP_FILE!"

:: Show the help file to the user
notepad "!HELP_FILE!"

goto :eof

:log_message
:: Helper function to log messages with timestamp
echo [%TIME%] %*
goto :eof