#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

gitdirs=$(find . -type d -name ".git")

printf "\n${CYAN}📊 Multi-Repo Status Summary${NC}\n"
printf "${YELLOW}%-35s │ %-13s │ %-11s │ %-8s${NC}\n" "Repository" "🗂️ Uncommitted" "⬆️ Unpushed" "⚠️ Behind"
printf "%0.s─" {1..75}; printf "\n"

total_uncommitted=0
total_ahead=0
total_behind=0

for gitdir in $gitdirs; do
    repo=$(dirname "$gitdir")
    cd "$repo" || continue
    uncommitted=$(git status --porcelain | wc -l | tr -d ' ')
    git remote update > /dev/null 2>&1
    if git rev-parse --abbrev-ref --symbolic-full-name @{u} > /dev/null 2>&1; then
        ahead=$(git rev-list --count @{u}..HEAD 2>/dev/null)
        behind=$(git rev-list --count HEAD..@{u} 2>/dev/null)
    else
        ahead=0
        behind=0
    fi
    printf "%-35s │ %-13s │ %-11s │ %-8s\n" "$repo" "$uncommitted" "$ahead" "$behind"
    total_uncommitted=$((total_uncommitted + uncommitted))
    total_ahead=$((total_ahead + ahead))
    total_behind=$((total_behind + behind))
    cd - > /dev/null || exit
done

printf "%0.s─" {1..75}; printf "\n"
printf "${GREEN}🟢 Overall: ${NC}🗂️ %d uncommitted file(s), ⬆️ %d unpushed commit(s), ⚠️ %d behind remote\n" "$total_uncommitted" "$total_ahead" "$total_behind" 