#!/bin/bash
# Modified by AI/LLM: 2026-09-30 - --local and --no-env install options.
#
# Project bootstrap script - Sets up Python development environment
#
# This script MUST be sourced to properly set up the environment:
#   source bootstrap
#
# Windows (PowerShell): dot-source bootstrap.ps1 instead
#   . .\bootstrap.ps1 [--env name] [--python 3.x.x] [-c|--clean] [--local] [--no-env]
#
# What it does:
# 1. Installs pyenv and required system dependencies if needed
# 2. Initializes pyenv and pyenv-virtualenv in the current shell
# 3. Creates a Python virtual environment named 'test_env' (or custom name)
# 4. Activates the virtual environment
# 5. Installs this repository (editable with the dev extra, unless --local)
#
# With --clean/-c option:
# - Deletes existing environment before creating a new one
#
# With --local:
# - Installs this repository as a normal package (pip install .), not editable
#
# With --no-env:
# - Skips pyenv and the virtual environment
# - Installs with pip --user so the commands run without activating an environment
#
# Usage:
#   source bootstrap [--pyenv] [-c|--clean] [--env custom_name] [--python 3.x.x] [--local] [--no-env]
#      ex. source bootstrap --pyenv -c --env my_env --python 3.10.6
#      ex. source bootstrap --local --no-env

# =============================================
# SOURCED GUARD
# =============================================
# Activation only persists when this file is sourced; executing it
# loses the environment when the child shell exits. `return` works
# at the top level only inside a sourced script.
if [ -z "${BASH_VERSION:-}" ]; then
    echo "ERROR: bootstrap is a bash script. Run it with bash:" >&2
    echo "Run: source bootstrap [--pyenv] [-c|--clean] [--env name] [--python 3.x.x] [--local] [--no-env]" >&2
    exit 1
fi
if ! (return 0 2>/dev/null); then
    echo "ERROR: bootstrap must be sourced, not executed." >&2
    echo "The environment would be lost when the child shell exits." >&2
    echo "Run: source bootstrap [--pyenv] [-c|--clean] [--env name] [--python 3.x.x] [--local] [--no-env]" >&2
    exit 1
fi

# =============================================
# VARIABLES
# =============================================
PYTHON_VERSION="3.13.8"  # Default Python version
export PYENV_NAME="test_env"   # Default environment name
FORCE_PYENV_INSTALL=false # Flag to force pyenv reinstallation
CLEAN_ENVIRONMENT=false   # Flag to delete and recreate environment
LOCAL_INSTALL=false       # Install this repo as a normal package, not editable
NO_ENV=false              # Skip the virtual environment and pip install --user
PYTHON_BIN="python"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"  # scripts root directory

# Source utilities (needed for echo_error, echo_status, etc.)
source "$SCRIPT_DIR/scripts/utils.sh"

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case "$1" in
        --pyenv)
            FORCE_PYENV_INSTALL=true
            shift
            ;;
        -c|--clean)
            CLEAN_ENVIRONMENT=true
            shift
            ;;
        --local)
            LOCAL_INSTALL=true
            shift
            ;;
        --no-env)
            NO_ENV=true
            shift
            ;;
        --env)
            if [ -n "$2" ]; then
                PYENV_NAME="$2"
                shift 2
            else
                echo_error "--env requires an environment name argument"
                return 1
            fi
            ;;
        --python)
            if [ -n "$2" ]; then
                PYTHON_VERSION="$2"
                shift 2
            else
                echo_error "--python requires a version number argument"
                return 1
            fi
            ;;
        *)
            echo_error "Unknown option: $1"
            echo "Usage: source bootstrap [--pyenv] [-c|--clean] [--env custom_name] [--python 3.x.x] [--local] [--no-env]"
            return 1
            ;;
    esac
done

# Export the environment name for use throughout the script
export PYENV_NAME

# =============================================
# FUNCTION DEFINITIONS
# =============================================

check_python_installed() {
    # First check if Python is installed at all
    if ! command -v python &> /dev/null && ! command -v python3 &> /dev/null; then
        echo_error "Python is not installed. Please install Python $PYTHON_VERSION before running this script."
        echo_error "On Ubuntu/Debian: sudo apt-get install python3"
        echo_error "On macOS: brew install python@$PYTHON_VERSION"
        echo_error "Or download from: https://www.python.org/downloads/"
        return 1
    fi

    # If pyenv is available, check if the specific version is installed
    if command -v pyenv &> /dev/null; then
        if ! pyenv versions | grep -q "$PYTHON_VERSION"; then
            echo_status "Python $PYTHON_VERSION is not available in pyenv. Installing it now..."
            if ! pyenv install "$PYTHON_VERSION"; then
                echo_error "Failed to install Python $PYTHON_VERSION via pyenv"
                echo_error "You may need to install build dependencies first or install it manually"
                return 1
            fi
        fi
    fi

    return 0
}

initialize_pyenv() {
    # Set up pyenv environment variables
    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"

    # Check if pyenv is already properly initialized
    if command -v pyenv &> /dev/null && pyenv virtualenvs &> /dev/null; then
        echo_status "pyenv is already properly initialized"
        return 0
    fi

    # Check if pyenv exists
    if [ ! -f "$PYENV_ROOT/bin/pyenv" ]; then
        echo_error "pyenv not found at $PYENV_ROOT/bin/pyenv"
        echo_error "Please install pyenv first or use --pyenv flag"
        return 1
    fi

    echo_status "Initializing pyenv..."

    # Clear any existing pyenv functions to avoid conflicts
    if declare -F | grep -q 'pyenv$'; then
        unset -f pyenv
    fi

    # Initialize pyenv and pyenv-virtualenv
    eval "$("$PYENV_ROOT/bin/pyenv" init -)" || {
        echo_error "Failed to initialize pyenv"
        return 1
    }

    # Only init virtualenv plugin if available
    if [ -f "$PYENV_ROOT/plugins/pyenv-virtualenv/bin/pyenv-virtualenv-init" ]; then
        eval "$("$PYENV_ROOT/bin/pyenv" virtualenv-init -)" || {
            echo_warning "Failed to initialize pyenv-virtualenv plugin"
        }
    fi

    # Verify initialization
    if ! pyenv virtualenvs &> /dev/null; then
        echo_error "Failed to initialize pyenv"
        return 1
    fi

    echo_status "pyenv initialized successfully"
    return 0
}

install_pyenv_dependencies() {
    # Install pyenv dependencies on Debian-based systems
    if [ -f /etc/debian_version ] || [ -f /etc/os-release ] && grep -q "ID=debian\|ID=ubuntu" /etc/os-release 2>/dev/null; then
        echo_status "Installing pyenv dependencies..."
        if ! command -v sudo &> /dev/null; then
            echo_error "sudo is required to install dependencies"
            return 1
        fi
        sudo apt-get update && sudo apt-get install -y \
            make build-essential libssl-dev zlib1g-dev \
            libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
            libncurses-dev xz-utils tk-dev libffi-dev liblzma-dev \
            python3-openssl git || {
            echo_error "Failed to install pyenv dependencies"
            echo_warning "You may need to install them manually"
            return 1
        }
    elif [ "$(uname)" = "Darwin" ]; then
        echo_status "Detected macOS - ensuring required tools are available"
        if ! command -v brew &> /dev/null; then
            echo_warning "Homebrew (brew) not found. You may need to install pyenv dependencies manually."
            echo_warning "Recommended: brew install pyenv"
            return 0
        fi
        if ! brew list pyenv &> /dev/null; then
            echo_status "Installing pyenv via homebrew..."
            brew install pyenv || {
                echo_error "Failed to install pyenv via homebrew"
                return 1
            }
        fi
    fi
    return 0
}

install_pyenv() {
    echo_status "Installing pyenv..."
    curl -fsSL https://pyenv.run | bash || {
        echo_error "Failed to install pyenv"
        return 1
    }
    return 0
}

check_and_install_pyenv() {
    # Check if pyenv is already available
    if command -v pyenv &> /dev/null && [ "$FORCE_PYENV_INSTALL" = false ]; then
        echo_status "pyenv is already available"
        return 0
    fi

    # Handle existing pyenv directory
    if [ -d "$HOME/.pyenv" ]; then
        if [ "$FORCE_PYENV_INSTALL" = true ]; then
            echo_status "Removing existing pyenv installation..."
            rm -rf "$HOME/.pyenv" || {
                echo_error "Failed to remove existing pyenv installation"
                return 1
            }
        else
            echo_status "Found existing pyenv directory"
        fi
    fi

    # Install dependencies first
    install_pyenv_dependencies || return 1

    # Install pyenv
    install_pyenv || return 1

    # Source the pyenv setup in current shell
    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$("$PYENV_ROOT/bin/pyenv" init -)"

    return 0
}

clean_environment() {
    if pyenv virtualenvs | grep -q "$PYENV_NAME"; then
        echo_status "Deleting existing virtual environment $PYENV_NAME..."
        pyenv uninstall -f "$PYENV_NAME" >/dev/null 2>&1 || {
            echo_error "Failed to delete virtual environment $PYENV_NAME"
            return 1
        }
    else
        echo_status "Virtual environment $PYENV_NAME does not exist, skipping deletion"
    fi
    return 0
}

install_python_version() {
    local version="$1"
    echo_status "Installing Python $version using pyenv..."
    pyenv install "$version" || {
        echo_error "Failed to install Python $version"
        return 1
    }
    return 0
}

check_environment_exists() {
    # Clean if requested
    if [ "$CLEAN_ENVIRONMENT" = true ]; then
        clean_environment || return 1
    fi

    # Check if Python version is available
    if ! pyenv versions | grep -q "$PYTHON_VERSION"; then
        install_python_version "$PYTHON_VERSION" || return 1
    fi

    # Create environment if it doesn't exist (exact name match:
    # a plain grep would also match "test_env2" when asked for "test_env")
    if ! pyenv virtualenvs --bare 2>/dev/null | grep -qx "$PYENV_NAME"; then
        echo_status "Creating virtual environment $PYENV_NAME..."
        pyenv virtualenv "$PYTHON_VERSION" "$PYENV_NAME" || {
            echo_error "Failed to create virtual environment $PYENV_NAME"
            return 1
        }
    else
        echo_status "Virtual environment $PYENV_NAME already exists"
    fi

    return 0
}

activate_environment() {
    echo_status "Activating $PYENV_NAME..."

    # Try pyenv activate first
    if command -v pyenv &> /dev/null; then
        # Get the virtualenv path
        local venv_path
        venv_path=$(pyenv prefix "$PYENV_NAME" 2>/dev/null)

        if [ -n "$venv_path" ] && [ -d "$venv_path" ]; then
            # Manually activate by sourcing the activate script
            local activate_script="$venv_path/bin/activate"
            if [ -f "$activate_script" ]; then
                source "$activate_script" || {
                    echo_error "Failed to source activation script: $activate_script"
                    return 1
                }
                echo_status "Successfully activated $PYENV_NAME"
                return 0
            fi
        fi

        # Fallback: try pyenv activate
        if pyenv activate "$PYENV_NAME" 2>/dev/null; then
            echo_status "Successfully activated $PYENV_NAME with pyenv"
            return 0
        fi
    fi

    # Last resort: check common virtualenv locations
    local possible_paths=(
        "$HOME/.pyenv/versions/$PYENV_NAME/bin/activate"
        "$HOME/.virtualenvs/$PYENV_NAME/bin/activate"
        "$SCRIPT_DIR/venv/bin/activate"
        "$SCRIPT_DIR/src/venv/bin/activate"
    )

    for path in "${possible_paths[@]}"; do
        if [ -f "$path" ]; then
            source "$path" || {
                echo_error "Failed to source activation script: $path"
                return 1
            }
            echo_status "Successfully activated $PYENV_NAME"
            return 0
        fi
    done

    echo_error "Failed to activate $PYENV_NAME - could not find activation script"
    return 1
}

install_requirements() {
    echo_status "Installing requirements..."

    # Check if pyproject.toml exists (could be in root or src/)
    local project_dir=""
    if [ -f "$SCRIPT_DIR/pyproject.toml" ]; then
        project_dir="$SCRIPT_DIR"
    elif [ -f "$SCRIPT_DIR/src/pyproject.toml" ]; then
        project_dir="$SCRIPT_DIR/src"
    fi

    if [ -n "$project_dir" ]; then
        local helper="$SCRIPT_DIR/scripts/bootstrap_install.py"
        if [ ! -f "$helper" ]; then
            echo_error "Missing $helper"
            return 1
        fi
        local flags=(--break-system-packages)
        if [ "$LOCAL_INSTALL" = true ]; then
            echo_status "Installing this repository as a normal package..."
            flags+=(--local)
        else
            echo_status "Installing package and dependencies from pyproject.toml..."
        fi
        if [ "$NO_ENV" = true ]; then
            flags+=(--no-env)
        fi
        local pip_words=()
        local word
        while IFS= read -r word; do
            if [ -n "$word" ]; then
                pip_words+=("$word")
            fi
        done < <("$PYTHON_BIN" "$helper" "${flags[@]}")
        if [ "${#pip_words[@]}" -eq 0 ]; then
            echo_error "Failed to resolve pip install arguments"
            return 1
        fi
        (cd "$project_dir" && "$PYTHON_BIN" -m pip "${pip_words[@]}") || {
            echo_error "Failed to install package and dependencies from pyproject.toml"
            return 1
        }
        return 0
    fi

    echo_warning "No pyproject.toml found"

    # Fallback: look for any requirements.txt files
    local req_files=()
    while IFS= read -r -d '' req_file; do
        req_files+=("$req_file")
    done < <(find "$SCRIPT_DIR" -name "requirements.txt" -print0)

    if [ ${#req_files[@]} -eq 0 ]; then
        echo_status "No requirements files found"
        return 0
    fi

    local user_args=()
    if [ "$NO_ENV" = true ]; then
        user_args+=(--user)
    fi
    for req_file in "${req_files[@]}"; do
        echo_status "Installing requirements from: $req_file"
        "$PYTHON_BIN" -m pip install "${user_args[@]}" -r "$req_file" --break-system-packages || {
            echo_error "Failed to install requirements from $req_file"
            return 1
        }
    done

    return 0
}

upgrade_pip() {
    echo_status "Upgrading pip..."
    local upgrade_args=(install --upgrade pip --break-system-packages)
    if [ "$NO_ENV" = true ]; then
        upgrade_args=(install --user --upgrade pip --break-system-packages)
    fi
    "$PYTHON_BIN" -m pip "${upgrade_args[@]}" || {
        echo_warning "Failed to upgrade pip"
        echo_warning "Continuing with requirements installation..."
    }
}

install_dev_packages() {
    echo_status "Installing local Python packages in development mode..."
    local install_script="$SCRIPT_DIR/scripts/install_packages.sh"

    if [ -f "$install_script" ]; then
        # Use source to run in current shell context, preserving environment variables
        (cd "$SCRIPT_DIR" && . "$install_script") || {
            echo_error "Dev packages not installed!"
            return 1
        }
    else
        echo_status "Install script not found at $install_script, attempting direct install..."
        # Fallback: install in editable mode from the repository root
        (cd "$SCRIPT_DIR" && pip install -e . --break-system-packages) || {
            echo_error "Failed to install package in development mode"
            return 1
        }
    fi
    return 0
}

sanitize_pytest_env() {
    # Ensure pytest outputs the short summary by sanitizing PYTEST_ADDOPTS
    # The short summary is needed for proper test failure parsing
    if [ -n "$PYTEST_ADDOPTS" ]; then
        # Remove --no-summary if present (pytest 7+ uses --no-summary to suppress short summary)
        # Also remove -S or --no-summary (older versions)
        local sanitized
        sanitized=$(echo "$PYTEST_ADDOPTS" | sed -e 's/--no-summary[[:space:]]*//g' -e 's/--no-summary//g' -e 's/-S[[:space:]]*//g' -e 's/-S//g')

        # Remove leading/trailing whitespace and any double spaces
        sanitized=$(echo "$sanitized" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/[[:space:]]\+/ /g')

        if [ "$sanitized" != "$PYTEST_ADDOPTS" ]; then
            echo_warning "Removing --no-summary from PYTEST_ADDOPTS to ensure test output is parsed correctly"
            export PYTEST_ADDOPTS="$sanitized"
        fi
    fi

    # Also unset FORCE_COLOR and PY_COLORS as they can interfere with output parsing
    if [ -n "$FORCE_COLOR" ]; then
        echo_warning "Unsetting FORCE_COLOR to ensure pytest output is parsed correctly"
        unset FORCE_COLOR
    fi
    if [ -n "$PY_COLORS" ]; then
        echo_warning "Unsetting PY_COLORS to ensure pytest output is parsed correctly"
        unset PY_COLORS
    fi

    return 0
}

# =============================================
# MAIN EXECUTION
# =============================================
if [ "$NO_ENV" = true ]; then
    echo_status "Starting project bootstrap (no virtual environment)"
else
    echo_status "Starting project bootstrap with environment: $PYENV_NAME"
fi

if [ "$LOCAL_INSTALL" = true ]; then
    echo_status "Local install: this repository, not editable"
fi
if [ "$NO_ENV" = true ]; then
    echo_status "No virtual environment: installing with --user"
fi

if [ "$NO_ENV" = true ]; then
    if ! command -v python >/dev/null 2>&1 && ! command -v python3 >/dev/null 2>&1; then
        echo_error "Python is not installed. Please install Python $PYTHON_VERSION before running this script."
        echo_error "Or download from: https://www.python.org/downloads/"
        return 1
    fi
    if command -v python >/dev/null 2>&1; then
        PYTHON_BIN="python"
    else
        PYTHON_BIN="python3"
    fi
else
    if [ "$FORCE_PYENV_INSTALL" = true ]; then
        echo_status "Forcing pyenv reinstallation"
    fi

    # 1. Check if Python is installed
    if ! check_python_installed; then
        return 1
    fi

    # 2. Check/install pyenv
    if ! check_and_install_pyenv; then
        return 1
    fi

    # 3. Initialize pyenv
    if ! initialize_pyenv; then
        return 1
    fi

    # 4. Check/create virtual environment
    if ! check_environment_exists; then
        return 1
    fi

    # 5. Activate the environment
    if ! activate_environment; then
        return 1
    fi
    PYTHON_BIN="python"
fi

# 5.5. Sanitize pytest environment to ensure proper output parsing
sanitize_pytest_env

# 6. Upgrade pip
upgrade_pip

# 7. Install this repository
if ! install_requirements; then
    return 1
fi

# Final verification
echo_status "Bootstrap completed successfully!"
if [ "$NO_ENV" = true ]; then
    scripts_dir=$("$PYTHON_BIN" -c "import sys, sysconfig; scheme = 'nt_user' if sys.platform == 'win32' else 'posix_user'; print(sysconfig.get_path('scripts', scheme))")
    echo_status "Installed for the current user (no virtual environment)"
    echo_status "Command directory: $scripts_dir"
    echo_status "If fiona-assistant is not found, add that directory to PATH."
    echo_status "Python: $("$PYTHON_BIN" --version) ($PYTHON_BIN)"
    echo_status "Pip: $("$PYTHON_BIN" -m pip --version)"
else
    echo_status "Virtual environment '$PYENV_NAME' is active"
    echo_status "Python: $(python --version) ($(which python))"
    echo_status "Pip: $(pip --version)"
fi
echo_status ""
echo_status "To verify installation, try:"
echo_status "  fiona-assistant test"
echo_status "  librarian --help"
echo_status "  mdoctor --help"
echo_status ""
echo_status "Happy coding!"
