#!/bin/bash

clear
GREEN='\033[1;32m'
RESET='\033[0m'
# Spinner Function
spinner() {
    local pid=$!
    local delay=0.1
    local spinstr='|/-\'
    while ps -p $pid > /dev/null 2>&1; do
        local temp=${spinstr#?}
        printf " [%c]  " "$spinstr"
        spinstr=$temp${spinstr%"$temp"}
        sleep $delay
        printf "\b\b\b\b\b\b"
    done
    printf "    \b\b\b\b"
}

echo -e "${GREEN}"
echo "=============================================================="
echo ""
echo "        ███╗   ██╗██╗████████╗██╗███████╗██╗  ██╗      "
echo "        ████╗  ██║██║╚══██╔══╝██║██╔════╝██║  ██║      "
echo "        ██╔██╗ ██║██║   ██║   ██║███████╗███████║      "
echo "        ██║╚██╗██║██║   ██║   ██║╚════██║██╔══██║      "
echo "        ██║ ╚████║██║   ██║   ██║███████║██║  ██║      "
echo "        ╚═╝  ╚═══╝╚═╝   ╚═╝   ╚═╝╚══════╝╚═╝  ╚═╝      "
echo ""
echo "        * Nitish Smart Git Automation Tool *"
echo ""
echo "=============================================================="
echo ""
echo -e "${RESET}"
echo ""
# Show Git Status
echo "📌 Checking Git Status..."
echo "--------------------------------------------------------------"
git status
echo "--------------------------------------------------------------"
echo ""

# Confirm Add
read -p "➜ Do you want to add all changes? (y/n): " confirm_add

if [[ "$confirm_add" != "y" ]]; then
    echo "❌ Operation cancelled."
    exit 1
fi

git add .

echo "✅ Files staged successfully."
echo ""

# Commit Message
read -p "📝 Enter Commit Message: " message

if [[ -z "$message" ]]; then
    echo "❌ Commit message cannot be empty."
    exit 1
fi

git commit -m "$message"

if [[ $? -ne 0 ]]; then
    echo "❌ Commit failed."
    exit 1
fi

echo "✅ Commit successful."
echo ""

# Detect Current Branch Automatically
branch=$(git branch --show-current)

echo "🌿 Current Branch: $branch"
echo "--------------------------------------------------------------"

echo "🚀 Pushing to origin $branch ..."

git push origin "$branch" &
spinner

if [[ $? -eq 0 ]]; then
    echo ""
    echo -e "${GREEN}=============================================================="
    echo "🎉 SUCCESS! Code pushed successfully!"
    echo "=============================================================="
    echo -e "${RESET}"
else
    echo ""
    echo "❌ Push failed. Please check credentials."
fi