#!/bin/bash
set -e

# Use the workspace target dir so hooks don't break when CARGO_TARGET_DIR points at a stale cache.
REPO_ROOT="$(git rev-parse --show-toplevel)"
cd "$REPO_ROOT"
export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$REPO_ROOT/target}"

echo "🏗️  Sruja Pre-commit Hook..."

# Find staged .rs files
STAGED_RS=$(git diff --cached --name-only --diff-filter=ACMR | grep '\.rs$' || true)

# Find staged .sruja files
STAGED_SRUJA=$(git diff --cached --name-only --diff-filter=ACMR | grep '\.sruja$' || true)

if [ -n "$STAGED_RS" ]; then
    echo "  🔍 Checking Rust formatting..."
    cargo fmt --all -- --check
    
    echo "  🔍 Linting Rust code..."
    cargo clippy --workspace -- -D warnings
fi

if [ -n "$STAGED_SRUJA" ]; then
    echo "  🔍 Linting Sruja files..."
    for file in $STAGED_SRUJA; do
        if [ -f "target/release/sruja" ]; then
            ./target/release/sruja lint "$file"
        else
            echo "  ⚠️  Sruja CLI not built (target/release/sruja missing). Skipping DSL linting."
        fi
    done
fi

echo "✅ All checks passed!"
exit 0
