#!/bin/bash

set -e

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

REQUIREMENTS_FILE="requirements.txt"

# --- Pre-requesites ---

# Check if the requirements.txt file exists in the current directory
if [ ! -f "$REQUIREMENTS_FILE" ]; then
    echo "[ERROR] '$REQUIREMENTS_FILE' not found in the current directory."
    exit 1
fi

echo "[INFO] Starting Python environment compatibility check for '$REQUIREMENTS_FILE'"

# Get a list of all installed pyenv versions.
# Filter to only include BASE Python versions from 3.8 to 3.14.
# The regex now specifically matches versions like "3.8.x", "3.9.x", ..., "3.14.x"
# and excludes virtual environments (which typically contain '/envs/' or are aliases).
PYENV_VERSIONS=$(pyenv versions --bare | grep -E "^3\.(8|9|1[0-4])(\.[0-9]+)?$")

# Check if any pyenv versions are found after filtering
if [ -z "$PYENV_VERSIONS" ]; then
    echo "[WARNGING] No base pyenv versions found matching the 3.8-3.14 range. Please ensure you have them installed with pyenv."
    exit 1
fi

# Initialize arrays to store successful and failed versions
SUCCESS_VERSIONS=()
FAILED_VERSIONS=()

# --- Main Loop: Iterate through each pyenv version ---
for VERSION in $PYENV_VERSIONS; do
    # Temporarily set the pyenv version for the current shell session.
    # Redirect all output to /dev/null to keep the loop silent.
    if ! pyenv shell "$VERSION" &> /dev/null; then
        FAILED_VERSIONS+=("$VERSION (activation failed)")
        continue # Move to the next version
    fi

    # Verify that 'pip-tools' is available in this environment.
    # Redirect all output to /dev/null.
    if ! python -c "import piptools" &> /dev/null; then
        FAILED_VERSIONS+=("$VERSION (pip-tools not found)")
        pyenv shell --unset &> /dev/null
        continue # Move to the next version
    fi

    # Attempt to resolve dependencies using 'python -m piptools compile'.
    # Redirect all output (both stdout and stderr) to /dev/null.
    # We only care about the exit status for success/failure.
    TEMP_OUTPUT_FILE=$(mktemp) # Create a temporary file for --output-file
    if python -m piptools compile "$REQUIREMENTS_FILE" --output-file "$TEMP_OUTPUT_FILE" &> /dev/null; then
        SUCCESS_VERSIONS+=("$VERSION")
    else
        FAILED_VERSIONS+=("$VERSION (Resolution failed)") # Only store a simple message
    fi
    rm -f "$TEMP_OUTPUT_FILE" # Clean up the temporary file

    # Unset the pyenv shell version. Redirect all output to /dev/null.
    pyenv shell --unset &> /dev/null
done

# --- Summary Report ---
echo "" # Add a newline for better separation from the start message
echo "--- Summary ---"
echo "Successfully resolved dependencies for:"
if [ ${#SUCCESS_VERSIONS[@]} -eq 0 ]; then
    echo "  None"
else
    for v in "${SUCCESS_VERSIONS[@]}"; do
        echo "  - $v"
    done
fi

echo ""
echo "Failed to resolve dependencies for:"
if [ ${#FAILED_VERSIONS[@]} -eq 0 ]; then
    echo "  None"
else
    for v in "${FAILED_VERSIONS[@]}"; do
        echo "  - $v" # Print the simple failure message
    done
fi

echo ""
echo "--- Script Finished ---"
