#!/bin/bash

# Pre-commit hook for Nextflow Parser
# Runs essential tests before allowing commits to grammar files

set -e

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

print_status() {
    local color=$1
    local message=$2
    echo -e "${color}${message}${NC}"
}

# Check if grammar files are being committed
grammar_files_changed=$(git diff --cached --name-only | grep -E '(grammars/.*\.(g4|ts)$|src/nextflow-parser/.*\.ts$)' | grep -v '__tests__' || true)

if [ -z "$grammar_files_changed" ]; then
    print_status $GREEN "No grammar files changed, skipping parser tests"
    exit 0
fi

print_status $YELLOW "Grammar files changed, running parser tests..."
echo "Changed files:"
echo "$grammar_files_changed" | sed 's/^/  /'
echo

# Build and run essential tests
print_status $YELLOW "Building project..."
npm run build

print_status $YELLOW "Running unit tests..."
npm run test:unit

print_status $YELLOW "Running property-based tests..."
npm run test:properties

print_status $YELLOW "Running regression tests..."
npm run test:regression

# Check baseline if it exists
if [ -f "nf-parser-testing/baseline-metrics.json" ]; then
    print_status $YELLOW "Running baseline comparison..."
    npm run test:nf-core:compare
fi

print_status $GREEN "✅ Pre-commit tests passed!"
exit 0