#!/usr/bin/env sh

# Fix PATH for GUI Git clients (SourceTree, Tower, etc.)
# These tools don't inherit the shell's PATH, so we need to add common tool locations
export PATH="/usr/local/bin:/opt/homebrew/bin:$HOME/.local/share/pnpm:$PATH"

# Try to find pnpm if not in PATH
if ! command -v pnpm >/dev/null 2>&1; then
  # Check common Corepack/Node locations
  if [ -d "$HOME/.nvm" ]; then
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
  fi

  # If still not found, exit with helpful error
  if ! command -v pnpm >/dev/null 2>&1; then
    echo "❌ pnpm not found in PATH"
    echo "If using a Git GUI, you may need to:"
    echo "  1. Restart your Git GUI after installing pnpm"
    echo "  2. Or commit from the terminal instead"
    exit 1
  fi
fi

# Check for uv (Python package manager)
if ! command -v uv >/dev/null 2>&1; then
  # Add Cargo bin directory for uv
  export PATH="$HOME/.cargo/bin:$PATH"

  if ! command -v uv >/dev/null 2>&1; then
    echo "⚠️  uv not found in PATH - Python type checking will be skipped"
    echo "Install uv: curl -LsSf https://astral.sh/uv/install.sh | sh"
  fi
fi

echo "Running pre-commit type checks..."

# TypeScript type check (affected files only)
echo "Checking TypeScript types..."
pnpm nx affected -t typecheck --base=HEAD~1 --parallel=1 || echo "⚠️  TypeScript errors found (non-blocking for now)"

# ESLint type-aware check
echo "Checking ESLint..."
pnpm nx affected -t lint --base=HEAD~1 --parallel=1 || echo "⚠️  ESLint errors found (non-blocking for now)"

# Python type check (production code only, if Python files changed)
if git diff --cached --name-only | grep -q "\.py$"; then
  echo "Checking Python types (production code only)..."

  # Check screenshare bot if its production files changed
  if git diff --cached --name-only | grep "screenshare-bot.*\.py$" | grep -v "test_" | grep -v "/tests/" | grep -q .; then
    echo "  → Checking screenshare-bot production code..."
    cd apps/slide-system/bots/screenshare-bot
    # Check only production files (exclude tests, scripts)
    # Run mypy and save output, then filter warnings and check exit code
    set -o pipefail  # Make pipeline fail if mypy fails
    uv run mypy bot.py screenshare_bot.py frame_processor.py lifecycle_manager.py \
      slide_control_listener.py slide_navigator.py screenshot_capture.py \
      deck_url_validator.py performance_monitor.py transports/*.py 2>&1 | \
      grep -v "^Warning: unused section" || {
        echo "❌ screenshare-bot type check failed"
        cd - > /dev/null
        exit 1
      }
    set +o pipefail  # Reset pipefail
    cd - > /dev/null
  fi

  # Check voice-agent-livekit if its production files changed
  if git diff --cached --name-only | grep "voice-agent-livekit.*\.py$" | grep -v "test_" | grep -v "/tests/" | grep -q .; then
    echo "  → Checking voice-agent-livekit production code..."
    cd apps/voice-agent-livekit
    # Run ruff for linting
    uv run ruff check src/ || {
      echo "❌ voice-agent-livekit ruff check failed"
      cd - > /dev/null
      exit 1
    }
    cd - > /dev/null
  fi

  # Check tool forwarder if its production files changed
  if git diff --cached --name-only | grep "tool-forwarder.*\.py$" | grep -v "test_" | grep -v "/tests/" | grep -q .; then
    echo "  → Checking tool-forwarder production code..."
    cd apps/slide-system/bots/tool-forwarder
    # Check production files only
    uv run ruff check bot.py railway_client.py tool_call_parser.py 2>&1 || {
      echo "❌ tool-forwarder ruff check failed"
      cd - > /dev/null
      exit 1
    }
    cd - > /dev/null
  fi
fi

echo "✅ All type checks passed!"
