#!/usr/bin/env bash
set -euo pipefail

# Interactive Rebase Conflict Resolution Helper
#
# Purpose:
#   Provide a safe, repeatable workflow for handling conflicts during an
#   interactive rebase without making destructive changes by default.
#
# What it does:
#   - Shows the current rebase status
#   - Optionally prints conflicted files
#   - Optionally opens files you specify in your editor
#   - Reminds you to verify staged changes before continuing
#   - Offers non-destructive commands for continue, abort, or skip
#
# What it does not do:
#   - It does not auto-resolve conflicts
#   - It does not stage files automatically
#   - It does not continue the rebase without your confirmation
#
# Usage:
#   ./interactive-rebase-helper.sh [--status] [--files] [--edit file1 file2 ...]
#   ./interactive-rebase-helper.sh [--continue] [--abort] [--skip]
#
# Notes:
#   - Run this only inside a Git repository.
#   - This script assumes a rebase is already in progress when using
#     continue/abort/skip actions.

print_usage() {
  cat <<'EOF'
Usage:
  interactive-rebase-helper.sh [options]

Options:
  --status              Show git status and rebase-related guidance
  --files               List conflicted files, if any
  --edit <files...>     Open one or more files in $EDITOR or vi
  --continue            Run 'git rebase --continue' after your manual checks
  --abort               Run 'git rebase --abort'
  --skip                Run 'git rebase --skip'
  -h, --help            Show this help text

Examples:
  ./interactive-rebase-helper.sh --status
  ./interactive-rebase-helper.sh --files
  ./interactive-rebase-helper.sh --edit path/to/file1 path/to/file2
  ./interactive-rebase-helper.sh --continue
EOF
}

require_git_repo() {
  if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
    echo "Error: this script must be run inside a Git repository." >&2
    exit 1
  fi
}

show_status() {
  echo "== Git status =="
  git status --short --branch || true
  echo
  echo "== Rebase guidance =="
  echo "1) Inspect conflicted files."
  echo "2) Resolve only the intended lines."
  echo "3) Stage the resolved files manually with: git add <files>"
  echo "4) Review staged diff with: git diff --staged"
  echo "5) Continue only when the result is correct."
}

list_conflicted_files() {
  echo "== Conflicted files =="
  local found=0
  while IFS= read -r file; do
    [[ -n "$file" ]] || continue
    found=1
    echo "$file"
  done < <(git diff --name-only --diff-filter=U)

  if [[ "$found" -eq 0 ]]; then
    echo "No conflicted files detected."
  fi
}

open_files() {
  if [[ "$#" -eq 0 ]]; then
    echo "Error: --edit requires at least one file path." >&2
    exit 1
  fi

  local editor="${EDITOR:-vi}"
  echo "Opening files with: $editor"
  "$editor" "$@"
}

continue_rebase() {
  echo "Before continuing, verify the staged result:"
  echo "  git diff --staged"
  read -r -p "Continue rebase now? [y/N] " answer
  case "$answer" in
    y|Y|yes|YES)
      git rebase --continue
      ;;
    *)
      echo "Cancelled. No changes were made.";
      ;;
  esac
}

abort_rebase() {
  read -r -p "Abort the current rebase? This will stop the rebase process. [y/N] " answer
  case "$answer" in
    y|Y|yes|YES)
      git rebase --abort
      ;;
    *)
      echo "Cancelled. No changes were made.";
      ;;
  esac
}

skip_commit() {
  read -r -p "Skip the current commit in the rebase? [y/N] " answer
  case "$answer" in
    y|Y|yes|YES)
      git rebase --skip
      ;;
    *)
      echo "Cancelled. No changes were made.";
      ;;
  esac
}

main() {
  require_git_repo

  if [[ "$#" -eq 0 ]]; then
    print_usage
    exit 0
  fi

  case "$1" in
    --help|-h)
      print_usage
      ;;
    --status)
      show_status
      ;;
    --files)
      list_conflicted_files
      ;;
    --edit)
      shift
      open_files "$@"
      ;;
    --continue)
      continue_rebase
      ;;
    --abort)
      abort_rebase
      ;;
    --skip)
      skip_commit
      ;;
    *)
      echo "Error: unknown option '$1'" >&2
      print_usage >&2
      exit 1
      ;;
  esac
}

main "$@"