#!/bin/bash
# Pre-push hook to generate PDFs before pushing
# This ensures PDFs are always up to date when code is pushed

# Capture remote from arguments
REMOTE="$1"

# Don't exit on error immediately - we want to report issues but not break the push
set +e

REPO_ROOT="$(git rev-parse --show-toplevel)"
DOCKER_DIR="$REPO_ROOT/docker"
PDFS_DIR="$REPO_ROOT/docker/resume/pdfs"
GENERATE_PDFS_SCRIPT="$DOCKER_DIR/generate-pdfs.js"

# Check if we're in a git repository
if [ -z "$REPO_ROOT" ]; then
    echo "Warning: Not in a git repository, skipping PDF generation"
    exit 0
fi

# Check if docker directory exists
if [ ! -d "$DOCKER_DIR" ]; then
    echo "Warning: docker directory not found, skipping PDF generation"
    exit 0
fi

# Check if generate-pdfs.js exists
if [ ! -f "$GENERATE_PDFS_SCRIPT" ]; then
    echo "Warning: generate-pdfs.js not found, skipping PDF generation"
    exit 0
fi

echo "Running pre-push hook: Generating PDFs..."

# Navigate to docker directory
cd "$DOCKER_DIR" || {
    echo "Error: Could not navigate to docker directory"
    exit 1
}

# Check if node_modules exists, install if not
if [ ! -d "node_modules" ]; then
    echo "Installing npm dependencies..."
    if ! PUPPETEER_SKIP_DOWNLOAD=true npm install; then
        echo "Error: Failed to install npm dependencies"
        exit 1
    fi
fi

# Check if npm is available
if ! command -v npm &> /dev/null; then
    echo "Error: npm is not installed or not in PATH"
    exit 1
fi

# Try to find Chrome executable
CHROME_PATH=""
if [ -f "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ]; then
    CHROME_PATH="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
elif command -v google-chrome &> /dev/null; then
    CHROME_PATH=$(which google-chrome)
elif command -v chromium &> /dev/null; then
    CHROME_PATH=$(which chromium)
fi

if [ -z "$CHROME_PATH" ]; then
    echo "Warning: Chrome not found, skipping PDF generation"
    echo "PDFs will be generated during Docker build instead"
    exit 0
fi

# Generate PDFs
echo "Generating PDFs using Chrome at: $CHROME_PATH"
if PUPPETEER_EXECUTABLE_PATH="$CHROME_PATH" npm run generate-pdfs; then
    echo "✓ PDFs generated successfully"
    
    # Stage the PDFs directory if it exists and has changes
    cd "$REPO_ROOT" || exit 1
    if [ -d "$PDFS_DIR" ]; then
        # Check if there are any PDF files
        PDF_COUNT=$(find "$PDFS_DIR" -name "*.pdf" -type f 2>/dev/null | wc -l | tr -d ' ')
        if [ "$PDF_COUNT" -gt 0 ]; then
            # Add all PDFs to staging
            git add "$PDFS_DIR" 2>/dev/null
            
            # Check if there are changes staged
            if ! git diff --cached --quiet --exit-code "$PDFS_DIR" 2>/dev/null; then
                echo "✓ PDFs updated. Committing changes..."
                
                # Commit the changes
                if git commit -m "Auto-update generated PDFs" --no-verify; then
                    echo "✓ Changes committed. Restarting push..."
                    
                    # Push the new commit
                    # We use --no-verify to skip running this hook again since we just ran it
                    if git push "$REMOTE" --no-verify; then
                        echo "✓ Push successful."
                        exit 1 # Cancel the original push since we just pushed the new state
                    else
                        echo "✗ Failed to push updated PDFs."
                        exit 1
                    fi
                else
                    echo "✗ Failed to commit updated PDFs."
                    exit 1
                fi
            fi
        fi
    fi
else
    echo "Warning: PDF generation failed, continuing with push"
    echo "PDFs will be generated during Docker build instead"
fi

exit 0
