#!/bin/bash
# Pre-commit hook for linting TypeScript and Rust code
# Only checks staged files, not the entire codebase
# Install: cp scripts/pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit

set -e

echo "Running pre-commit checks..."

# Get the root directory of the repo
ROOT_DIR=$(git rev-parse --show-toplevel)

# Check for staged TypeScript/Vue files
TS_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(ts|vue)$' | grep -v node_modules || true)

if [ -n "$TS_FILES" ]; then
    echo "Linting staged TypeScript/Vue files..."
    cd "$ROOT_DIR"
    # Only lint the staged files, not the entire codebase
    echo "$TS_FILES" | xargs npx eslint --max-warnings=0 2>/dev/null || {
        echo "ESLint found errors in staged files. Please fix them before committing."
        exit 1
    }
fi

# Check for staged Rust files
RS_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.rs$' || true)

if [ -n "$RS_FILES" ]; then
    echo "Checking Rust formatting..."
    cd "$ROOT_DIR/src-tauri"
    cargo fmt --check || {
        echo "Rust formatting check failed. Run 'cargo fmt' to fix."
        exit 1
    }
fi

echo "Pre-commit checks passed."
