#!/bin/bash

# Git Prepare-commit-msg Hook - Node.js Wrapper
# This is a minimal wrapper that calls the actual Node.js implementation
# Why: Allows the Node.js script to use relative imports from its installed location

set -e

# Colors for output
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

# Convert Windows path to Git Bash/WSL path
# Why: npm prefix -g in Git Bash returns Windows paths (C:\...) that need conversion
convert_windows_path() {
    local path="$1"

    # Check if it's a Windows path (contains :\ or starts with C:)
    if [[ "$path" =~ ^[A-Za-z]:\\ ]] || [[ "$path" =~ ^[A-Za-z]: ]]; then
        # Convert C:\path\to\file to /c/path/to/file
        # First, extract drive letter
        local drive=$(echo "$path" | sed 's/^\([A-Za-z]\):.*/\1/' | tr '[:upper:]' '[:lower:]')
        # Remove drive letter and colon, replace backslashes with forward slashes
        local rest=$(echo "$path" | sed 's/^[A-Za-z]://' | sed 's/\\/\//g')
        echo "/$drive$rest"
    else
        echo "$path"
    fi
}

# Function to find the Node.js script
find_hook_script() {
    # Why: Try multiple locations to find where npm installed the package
    # Checks: global npm, local node_modules, npm prefix

    # Get npm global prefix and convert if it's a Windows path
    local npm_prefix=$(npm prefix -g 2>/dev/null || echo "")
    if [ -n "$npm_prefix" ]; then
        npm_prefix=$(convert_windows_path "$npm_prefix")
    fi

    local SCRIPT_PATHS=(
        # Global npm installation (Linux/Mac)
        "/usr/local/lib/node_modules/claude-git-hooks/lib/hooks/prepare-commit-msg.js"
        "/usr/lib/node_modules/claude-git-hooks/lib/hooks/prepare-commit-msg.js"
        # Global npm installation (Windows) - node_modules directly under prefix
        "${npm_prefix}/node_modules/claude-git-hooks/lib/hooks/prepare-commit-msg.js"
        # Global npm installation (Unix/WSL) - lib/node_modules under prefix
        "${npm_prefix}/lib/node_modules/claude-git-hooks/lib/hooks/prepare-commit-msg.js"
        # Home directory npm global
        "$HOME/.npm-global/lib/node_modules/claude-git-hooks/lib/hooks/prepare-commit-msg.js"
        # Local node_modules (if linked or installed locally)
        "./node_modules/claude-git-hooks/lib/hooks/prepare-commit-msg.js"
        "../node_modules/claude-git-hooks/lib/hooks/prepare-commit-msg.js"
    )

    for path in "${SCRIPT_PATHS[@]}"; do
        if [ -f "$path" ]; then
            echo "$path"
            return 0
        fi
    done

    return 1
}

# Early validation: Check if .claude directory exists
# Why: Provides helpful error message if installation incomplete or in wrong directory
REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo "")
if [ -z "$REPO_ROOT" ]; then
    echo -e "${RED}Error: Not in a git repository${NC}" >&2
    exit 1
fi

if [ ! -d "$REPO_ROOT/.claude" ]; then
    echo -e "${RED}Error: .claude directory not found${NC}" >&2
    echo "" >&2
    echo "Claude Git Hooks installation is incomplete or missing." >&2
    echo "" >&2
    echo "Current directory: $(pwd)" >&2
    echo "Repository root:   $REPO_ROOT" >&2
    echo "" >&2
    echo "Common cause: Installation performed from wrong directory" >&2
    echo "" >&2
    echo "Solution:" >&2
    echo "  cd $REPO_ROOT" >&2
    echo "  claude-hooks install --force" >&2
    exit 1
fi

# Find the Node.js script
NODE_HOOK=$(find_hook_script)

if [ -z "$NODE_HOOK" ]; then
    echo -e "${RED}Error: Could not find prepare-commit-msg.js${NC}" >&2
    echo "" >&2
    echo "Claude Git Hooks installation is incomplete." >&2
    echo "" >&2
    echo "Current directory: $(pwd)" >&2
    echo "Repository root:   $REPO_ROOT" >&2
    echo "" >&2
    echo "Common cause: Installation from wrong directory or npm package not properly installed" >&2
    echo "" >&2
    echo "Solution:" >&2
    echo "  # Verify you're in repository root" >&2
    echo "  cd $REPO_ROOT" >&2
    echo "  # Verify npm package is installed globally" >&2
    echo "  npm list -g claude-git-hooks" >&2
    echo "  # Reinstall hooks" >&2
    echo "  claude-hooks install --force" >&2
    exit 1
fi

# Execute the Node.js script
# Why: Pass all arguments ($@) - Git provides:
#   $1: commit message file path
#   $2: commit source (message, template, merge, squash, commit)
#   $3: commit SHA (for -c, -C, --amend)
exec node "$NODE_HOOK" "$@"
