# epad_signature_pad_hid_driver Python Environment

# Essential venv activation (must be included)
# Only set VIRTUAL_ENV_PROMPT and activate if venv actually exists
if [[ -d ".venv" ]]; then
    export VIRTUAL_ENV_PROMPT="($(basename "$PWD"))"
    source .venv/bin/activate
elif [[ -d "venv" ]]; then
    export VIRTUAL_ENV_PROMPT="($(basename "$PWD"))"
    source venv/bin/activate
fi

# Clear the "direnv: loading..." line
printf "\033[A\033[2K"

echo ""
echo "┌───────────────────────────────────────────────────────────────────────┐"
echo -e "│                        \033[35m🐍 epad-signature-pad-hid-driver\033[0m                           │"
echo "└───────────────────────────────────────────────────────────────────────┘"
echo "│"
echo "│  🚀 Quick Start:"
echo "│  ──────────────"
echo -e "│     \033[3;33mepad-signature-pad-hid-driver\033[0m                # Run CLI tool"
echo -e "│     \033[3;33muv run pytest\033[0m              # Run tests"
echo -e "│     \033[3;33muv run python -m epad_signature_pad_hid_driver.cli --help\033[0m"
echo "│"
echo "│  🧪 Testing:"
echo "│  ──────────"
echo -e "│     \033[3;33muv run pytest\033[0m              # Run all tests"
echo -e "│     \033[3;33muv run pytest -v\033[0m           # Verbose output"
echo -e "│     \033[3;33muv run pytest --cov\033[0m        # With coverage report"
echo "│"
echo "│  🔧 Code Quality:"
echo "│  ───────────────"
echo -e "│     \033[3;33muv run ruff check .\033[0m        # Lint code"
echo -e "│     \033[3;33muv run ruff format .\033[0m       # Format code"
echo -e "│     \033[3;33muv run mypy src/\033[0m           # Type check"
echo "│"
echo "│  📦 Package Management:"
echo "│  ─────────────────────"
echo -e "│     \033[3;33muv pip install <pkg>\033[0m       # Install package"
echo -e "│     \033[3;33muv add <package>\033[0m           # Add to dependencies"
echo -e "│     \033[3;33muv pip list\033[0m                # List installed packages"
echo "│"
echo "│  🔄 Development:"
echo "│  ──────────────"
echo -e "│     \033[3;33muv pip install -e .\033[0m        # Reinstall in editable mode"
echo -e "│     \033[3;33muv pip install -e '.[dev]'\033[0m # With dev dependencies"
echo "│"
echo "│  ℹ️  Info:"
echo "│  ────────"
echo -e "│     Python: \033[36m$(uv run python --version 2>&1 | cut -d' ' -f2)\033[0m"
echo -e "│     Binary: \033[36m$(which python)\033[0m"
echo -e "│     Project: \033[36m$(basename "$PWD")\033[0m"
echo -e "│     Version: \033[36m$(uv run python -c "import epad_signature_pad_hid_driver; print(epad_signature_pad_hid_driver.__version__)" 2>/dev/null || echo "0.1.0")\033[0m"
echo "│"
echo "└───────────────────────────────────────────────────────────────────────┘"
echo ""
echo "│  ℹ️  Environment Info:"
echo "│  ─────────────────────"
echo -e "│     Machine: \033[36m$(hostname)\033[0m"
echo -e "│     Kernel:  \033[36m$(uname -r)\033[0m"
echo -e "│     Shell:   \033[36m$SHELL\033[0m"
echo "│"
echo "│  📁 VSCode Settings Check:"
echo "│  ─────────────────────────"
if [[ "$(uname -s)" == "Darwin" ]]; then
    # macOS - check local app settings
    if grep -q "titleBar.activeBackground" "$HOME/Library/Application Support/Code/User/settings.json" 2>/dev/null; then
        echo -e "│     VSCode:  \033[32m✓ Configured\033[0m"
    else
        echo -e "│     VSCode:  \033[33m✗ Not configured\033[0m"
    fi
    if grep -q "titleBar.activeBackground" "$HOME/Library/Application Support/Cursor/User/settings.json" 2>/dev/null; then
        echo -e "│     Cursor:  \033[32m✓ Configured\033[0m"
    else
        echo -e "│     Cursor:  \033[33m✗ Not configured\033[0m"
    fi
else
    # Linux/WSL - check remote server settings
    if [[ -f "$HOME/.vscode-server/data/Machine/settings.json" ]]; then
        echo -e "│     VSCode:  \033[32m✓ Found\033[0m"
    else
        echo -e "│     VSCode:  \033[33m✗ Not found\033[0m"
    fi
    if [[ -f "$HOME/.cursor-server/data/Machine/settings.json" ]]; then
        echo -e "│     Cursor:  \033[32m✓ Found\033[0m"
    else
        echo -e "│     Cursor:  \033[33m✗ Not found\033[0m"
    fi
fi
echo "│"
echo "│  🔗 Global CLI Status:"
echo "│  ────────────────────"
# NOTE: direnv evaluates this file with bash, where `local` outside a function is an
# error ("local: can only be used in a function") AND leaves the variable unset -- so
# these are plain assignments with a _uvt_ prefix rather than `local`.
if command -v uv &>/dev/null; then
    _uvt_line=$(uv tool list --show-paths 2>/dev/null | grep -iE "^epad_signature_pad_hid_driver[[:space:]]|^epad-signature-pad-hid-driver[[:space:]]" | head -n 1)
    if [[ -n "$_uvt_line" ]]; then
        _uvt_version=$(echo "$_uvt_line" | awk '{print $2}')
        _uvt_env=$(echo "$_uvt_line" | sed -n 's/.*(\(.*\))$/\1/p')
        echo -e "│     Tool:    \033[32m✓ Installed via uv tool ($_uvt_version)\033[0m"

        # Editable vs frozen, from PEP 610 direct_url.json. A frozen install ignores
        # every edit made in this directory until it is reinstalled, so say so here
        # rather than leave it to be discovered an hour into debugging.
        _uvt_durl=$(find "$_uvt_env/lib" -maxdepth 4 -name direct_url.json 2>/dev/null | head -n 1)
        if [[ -z "$_uvt_durl" ]]; then
            echo -e "│     Mode:    \033[33mREGISTRY\033[0m - from PyPI, not from this checkout"
        elif grep -q '"editable"[[:space:]]*:[[:space:]]*true' "$_uvt_durl"; then
            echo -e "│     Mode:    \033[32mEDITABLE\033[0m - your edits take effect immediately"
        else
            echo -e "│     Mode:    \033[31mFROZEN\033[0m - edits here do NOT change what runs"
            echo -e "│               \033[3;33muv_tool_reinstall_current_project --frozen\033[0m to update it"
        fi

        # Show executable paths
        _uvt_in_tool=false
        while IFS= read -r _uvt_row; do
            if [[ "$_uvt_in_tool" == true ]]; then
                if [[ "$_uvt_row" == -\ * ]]; then
                    _uvt_exec=$(echo "$_uvt_row" | sed 's/^- //' | awk '{print $1}')
                    echo -e "│     Binary:  \033[36m~/.local/bin/$_uvt_exec\033[0m"
                else
                    break
                fi
            fi
            if echo "$_uvt_row" | grep -qiE "^epad_signature_pad_hid_driver[[:space:]]|^epad-signature-pad-hid-driver[[:space:]]"; then
                _uvt_in_tool=true
            fi
        done <<< "$(uv tool list --show-paths 2>/dev/null)"
    else
        echo -e "│     Tool:    \033[33m✗ Not installed globally\033[0m"
        echo -e "│     Hint:    \033[3;33muv_tool_install_current_project dev\033[0m"
    fi
else
    echo -e "│     Tool:    \033[31m✗ uv not found\033[0m"
fi
echo "│"
echo "└───────────────────────────────────────────────────────────────────────┘"
echo ""
