#!/usr/bin/env bash
# This file is managed by Terraform in github-control repository
# Do not edit this file, all changes will be overwritten
# If you need to change this file, create a pull request in
# https://github.com/infrahouse/github-control

set -e

echo "🔍 Running pre-commit checks..."

# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
    echo "❌ Not in a git repository"
    exit 1
fi

# Get the root of the git repository
REPO_ROOT=$(git rev-parse --show-toplevel)
cd "$REPO_ROOT"

# 1. Terraform formatting check
echo "📝 Checking Terraform formatting..."
if ! terraform fmt -check -recursive; then
    echo "❌ Terraform files need formatting"
    echo "   Run: terraform fmt -recursive"
    exit 1
fi
echo "✅ Terraform formatting OK"

# 2. Update terraform-docs
if command -v terraform-docs &> /dev/null; then
    echo "📚 Updating terraform documentation..."

    # Run terraform-docs
    if terraform-docs . > /dev/null 2>&1; then
        # Check if README.md changed
        if git diff --quiet README.md; then
            echo "✅ Documentation up to date"
        else
            echo "📝 README.md updated with terraform-docs"
            git add README.md
            echo "✅ Documentation changes staged"
        fi
    else
        echo "❌ Failed to generate terraform-docs"
        exit 1
    fi
else
    echo "⚠️  terraform-docs not installed"
    echo "   Install it: https://terraform-docs.io/user-guide/installation/"
    echo "   Or: brew install terraform-docs"
    echo ""
    echo "⚠️  Skipping documentation check"
fi

# 3. Check that all staged files end with a newline
echo "📝 Checking trailing newlines..."
MISSING_NEWLINE=()
for f in $(git diff --cached --name-only --diff-filter=ACM); do
    # Skip binary files
    if file --mime-encoding "$f" 2>/dev/null | grep -q binary; then
        continue
    fi
    # Check if file ends with a newline
    if [ -s "$f" ] && [ -n "$(tail -c 1 "$f" 2>/dev/null)" ]; then
        MISSING_NEWLINE+=("$f")
    fi
done
if [ ${#MISSING_NEWLINE[@]} -gt 0 ]; then
    echo "❌ The following files do not end with a newline:"
    for f in "${MISSING_NEWLINE[@]}"; do
        echo "   $f"
    done
    exit 1
fi
echo "✅ All files end with a newline"

echo ""
echo "✨ All pre-commit checks passed!"
echo "Happy coding!"
