#!/bin/bash

# Ensure repository has at least one commit (skip on initial commit)
git rev-parse --verify HEAD >/dev/null 2>&1 || exit 0

# Check for direct commits to main
branch=$(git branch --show-current)
if [[ "$branch" == "main" ]]; then
    echo -e "\e[31mERR:\e[0m Direct commits to main are not allowed.\n"
    echo -e "Switch to a feature branch and raise a PR.\n"
    exit 1
fi

# Check for .terraform/ in staged files
if git diff --cached --name-only | grep -q '\.terraform/'; then
    echo -e "\e[31mERR:\e[0m Staged .terraform/ directory detected.\n"
    echo -e "Add .terraform/ to .gitignore and unstage it.\n"
    exit 1
fi

# Check for conflict markers in staged files
if git diff --cached | grep -q '^+.*<<<<<<<'; then
    echo -e "\e[31mERR:\e[0m Conflict markers detected in staged files.\n"
    echo -e "Resolve all merge conflicts before committing.\n"
    exit 1
fi

exit 0