#!/bin/bash
set -e

# 1. Stage everything
git add -A

# 2. Get list of staged files
FILES=$(git diff --cached --name-only)

if [ -z "$FILES" ]; then
  echo "No changes to commit."
  exit 0
fi

# 3. Loop file by file
for FILE in $FILES; do
  # Get diff for this file
  DIFF=$(git diff --cached -- "$FILE")

  if [ -z "$DIFF" ]; then
    continue
  fi

  # Ask Ollama for a commit message describing this file change
  MSG=$(echo "$DIFF" | ollama run gemma3 \
    "You are a commit bot. Write a SHORT, clear, concise Git commit message for changes in file: $FILE.
Only output the commit message, nothing else.
Diff:
$DIFF")

  # Commit just this file with its message
  git commit -m "$MSG" -- "$FILE"

  echo "✅ Committed $FILE with message:"
  echo "$MSG"
done
