#!/bin/bash
export GIT_CEILING_DIRECTORIES="/Users/rbutare/websites"
BASE="/Users/rbutare/websites/apps/aiwizard"
cd $BASE
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; NC='\033[0m'
get_branch(){ git branch --show-current 2>/dev/null | tr -d '\n'; }
get_tag(){ git describe --tags --abbrev=0 2>/dev/null || echo "no tag"; }
get_last(){ git log --oneline -1 2>/dev/null; }
get_owner(){ ls -ld . 2>/dev/null | awk '{print $3}'; }
get_counts(){ ./tools/code.count.sh 2>/dev/null | tr '\n' ' '; }

show_header(){
  clear
  echo -e "${CYAN}================================================================${NC}"
  echo -e "  GIT MENU - aiwizard - $(whoami)"
  echo -e "${CYAN}================================================================${NC}"
  printf "%-10s %-28s %-8s %s\n" "Branch:" "$(get_branch)" "Owner:" "$(get_owner)"
  printf "%-10s %-28s %-8s %s\n" "Tag:" "$(get_tag)" "Tests:" "$(./tools/test.all.sh all 2>&1 | grep -E 'PASS.*FAIL' | tail -1)"
  echo "Last: $(get_last)"; echo "Counts: $(get_counts)"
  echo -e "${CYAN}----------------------------------------------------------------${NC}"
}

show_status(){
  show_header
  echo -e "${GREEN}1. CURRENT STATUS${NC}"; echo ""
  printf "  %-15s %s\n" "Branch:" "$(get_branch)"
  printf "  %-15s %s\n" "Tag:" "$(get_tag)"
  printf "  %-15s %s\n" "Owner:" "$(get_owner) (must be rbutareOld)"
  echo ""; git status --short 2>&1 | grep -v "warning: unable" | grep -v "Permission denied"
  echo ""; ./tools/code.count.sh 2>/dev/null; echo ""; ./tools/test.all.sh all 2>&1 | tail -10
}

show_versions(){
  show_header
  echo -e "${GREEN}2. VERSIONS${NC}"; echo ""
  printf "${YELLOW}%-25s | %-25s${NC}\n" "TAGS" "BRANCHES"
  echo "------------------------- | -------------------------"
  paste <(git tag --sort=-v:refname 2>/dev/null | head -12) <(git branch -a 2>/dev/null | head -12) | awk '{printf "  %-23s | %s\n",$1,$2}'
  echo ""; echo "Log:"; git log --oneline --graph --all --decorate -12 2>&1 | grep -v warning
}

what_changed(){
  show_header
  echo -e "${GREEN}3. WHAT CHANGED${NC}"; echo ""
  LAST=$(get_tag); [ "$LAST" = "no tag" ] && LAST="HEAD~1"
  echo "Since: $LAST -> HEAD"; echo ""
  echo "--- Files changed ---"; git diff $LAST --stat 2>&1 | grep -v warning | grep -v "unable"
  echo ""; echo "--- Commits since tag ---"; git log $LAST..HEAD --oneline 2>&1 | grep -v warning
  echo ""; echo "Suggested: v1.8 - <what you did> - $(git diff $LAST --shortstat 2>&1 | grep -v warning)"
}

while true; do
  show_header
  echo "  ACTIONS                                  | INFO / GIT COMMAND"
  echo "  -----------------------------------------|----------------------------------------"
  echo "  1) Current status                        | git status, test.all.sh, code.count.sh"
  echo "  2) Show versions/tags/branches/log       | git tag --sort, branch -a, log --graph"
  echo "  3) What changed? (for msg/tag)           | git diff LAST --stat, log LAST..HEAD"
  echo "  4) Update to next version                | add -A, commit -m vX.Y, tag -a vX.Y"
  echo "  5) Go back to previous version           | checkout -b <branch> <tag>"
  echo "  6) Quick commit                          | status + diff + add + commit"
  echo "  7) Fix ownership rbutareOld               | chown rbutareOld, xattr -cr"
  echo "  8) Run tests                             | ./tools/test.all.sh all"
  echo "  9) Exit"
  echo ""
  read -p "Choice [1-9]: " C
  case $C in
    1) show_status;;
    2) show_versions;;
    3) what_changed;;
    4) show_header; read -p "Next ver (1.8): " V; read -p "What did you do?: " M; git add -A 2>&1 | grep -v warning; git status --short 2>&1 | grep -v warning; read -p "Commit v$V? y/n: " Y; [ "$Y" = "y" ] && git commit -m "v$V - $M" 2>&1 | grep -v warning && git tag -a v$V -m "v$V $M" 2>&1 | grep -v warning;;
    5) show_header; git tag --sort=-v:refname 2>&1 | grep -v warning | cat -n | head -15; echo ""; read -p "Tag to go back to (v1.6): " T; read -p "New branch name (v1.6-fix): " B; [ -n "$T" ] && git checkout -b ${B:-$T-fix} $T 2>&1 | grep -v warning;;
    6) show_header; git status --short 2>&1 | grep -v warning; echo ""; git diff --stat 2>&1 | grep -v warning; read -p "msg: " M; [ -n "$M" ] && git add -A && git commit -m "$M" 2>&1 | grep -v warning;;
    7) sudo chown -R $(whoami):staff $BASE; sudo chown -R $(whoami):staff /Users/rbutare/dev/system 2>/dev/null; sudo xattr -cr $BASE; sudo xattr -cr /Users/rbutare/dev/system 2>/dev/null; git config --global --add safe.directory $BASE; echo "Fixed: $(get_owner)";;
    8) show_header; ./tools/test.all.sh all 2>&1 | grep -v warning;;
    9) clear; exit 0;;
  esac
  echo ""; read -p "ENTER to return..."
done
