#!/bin/sh
# This script installs the pre-commit hook for automatic code formatting

cat > .git/hooks/pre-commit <<'EOF'
#!/bin/sh
echo "🔍 Running spotlessApply before commit..."
./gradlew spotlessApply

# Check if spotlessApply failed
if [ $? -ne 0 ]; then
    echo "❌ Spotless formatting failed! Please fix the errors and try again."
    exit 1
fi

# Re-add files that may have been changed by Spotless
git add -u

echo "✅ Spotless formatting applied."

echo "🔍 Running detekt static analysis..."
./gradlew detekt

# Check if detekt failed (errors found)
if [ $? -ne 0 ]; then
    echo "❌ Detekt found errors! Please fix them before committing."
    echo "💡 Open the HTML report for details (path shown above)."
    exit 1
fi

echo "✅ Detekt passed."

#echo "🔍 Checking i18n keys..."
#./gradlew desktop:checkI18nKeys
#
## Check if i18n check failed
#if [ $? -ne 0 ]; then
#    echo "❌ i18n key check failed! Please fix missing or inconsistent keys before committing."
#    exit 1
#fi
#
#echo "✅ i18n keys check passed."
#EOF

# Make the hook executable
chmod +x .git/hooks/pre-commit

echo "✅ Pre-commit hook installed successfully!"
echo "📝 The hook will run './gradlew spotlessApply' and './gradlew detekt' before each commit."
