#!/bin/bash
# GitHub Push Hook - Triggered by /github command
# This hook commits all changes and pushes to GitHub

set -euo pipefail

# Read input from stdin (Claude Code passes JSON)
INPUT=$(cat)

# Extract the commit message if provided
COMMIT_MSG=$(echo "$INPUT" | jq -r '.arguments // empty' 2>/dev/null || echo "")

# Get the working directory
WORK_DIR=$(pwd)

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

# Check for changes
if git diff --quiet && git diff --staged --quiet && [ -z "$(git ls-files --others --exclude-standard)" ]; then
    echo '{"status": "no_changes", "message": "No changes to commit"}'
    exit 0
fi

# Get current branch
BRANCH=$(git rev-parse --abbrev-ref HEAD)

# Get a summary of changes for the output
CHANGED_FILES=$(git status --short | head -20)
FILE_COUNT=$(git status --short | wc -l | tr -d ' ')

# Output status for Claude to process
cat << EOF
{
  "status": "ready",
  "branch": "$BRANCH",
  "file_count": $FILE_COUNT,
  "changes_preview": $(echo "$CHANGED_FILES" | jq -Rs .),
  "commit_message": $(echo "$COMMIT_MSG" | jq -Rs .),
  "action": "Please proceed with: git add -A && git commit && git push origin $BRANCH"
}
EOF
