#!/bin/bash
# Post-merge hook to run linter after merging

echo "Running post-merge linter check..."

# Detect project type and run appropriate linter
if [ -f "package.json" ]; then
    echo "📦 Detected Node.js project"
    if command -v npm &> /dev/null && [ -f "package.json" ]; then
        if grep -q '"lint"' package.json; then
            echo "Running npm run lint..."
            npm run lint
            if [ $? -ne 0 ]; then
                echo "⚠️  Linting failed after merge. Please fix issues."
                exit 0  # Don't block the merge, just warn
            fi
        else
            echo "ℹ️  No lint script found in package.json"
        fi
    fi
elif [ -f "*.csproj" ] || [ -f "*.sln" ]; then
    echo "🔷 Detected .NET project"
    if command -v dotnet &> /dev/null; then
        echo "Running dotnet format..."
        dotnet format --verify-no-changes
        if [ $? -ne 0 ]; then
            echo "⚠️  Formatting issues detected after merge. Run 'dotnet format' to fix."
            exit 0
        fi
    fi
elif [ -f "requirements.txt" ] || [ -f "setup.py" ] || [ -f "pyproject.toml" ]; then
    echo "🐍 Detected Python project"
    if command -v flake8 &> /dev/null; then
        echo "Running flake8..."
        flake8 .
        if [ $? -ne 0 ]; then
            echo "⚠️  Linting issues detected after merge. Please fix."
            exit 0
        fi
    elif command -v pylint &> /dev/null; then
        echo "Running pylint..."
        pylint **/*.py
        if [ $? -ne 0 ]; then
            echo "⚠️  Linting issues detected after merge. Please fix."
            exit 0
        fi
    fi
fi

echo "✅ Post-merge linter check completed"
exit 0
