#!/usr/bin/env bash
# ssr-completions — install/uninstall shell completions for ssr.
#
# Shell -> target dir (user-scoped, no sudo):
#   zsh   ~/.zsh/completions/_ssr           (added to $fpath via shell hint)
#   bash  ~/.local/share/bash-completion/completions/ssr
#   fish  ~/.config/fish/completions/ssr.fish

set -euo pipefail
SSR_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# shellcheck source=../lib/common.sh
source "$SSR_ROOT/lib/common.sh"

ZSH_SRC="$SSR_ROOT/completions/_ssr"
BASH_SRC="$SSR_ROOT/completions/ssr.bash"
FISH_SRC="$SSR_ROOT/completions/ssr.fish"

ZSH_DST="$HOME/.zsh/completions/_ssr"
BASH_DST="$HOME/.local/share/bash-completion/completions/ssr"
FISH_DST="$HOME/.config/fish/completions/ssr.fish"

usage() {
    cat <<EOF
Usage: ssr completions <install|uninstall|status> [--shell zsh|bash|fish|all]

Install or remove shell completions for ssr.

Without --shell, auto-detects from \$SHELL and installs only that one.
Use --shell all to install for every supported shell.
EOF
}

detect_shell() {
    case "${SHELL:-}" in
        */zsh)  echo zsh ;;
        */bash) echo bash ;;
        */fish) echo fish ;;
        *)      echo "" ;;
    esac
}

install_zsh() {
    ssr::ensure_dir "$(dirname "$ZSH_DST")"
    ln -sfn "$ZSH_SRC" "$ZSH_DST"
    ssr::ok "zsh:  $ZSH_DST"
    # Hint: shell rc needs fpath entry. Detect if already configured.
    if ! grep -qsE "(\.zsh/completions|fpath\+?=.*\.zsh/completions)" "$HOME/.zshrc" 2>/dev/null; then
        cat <<'HINT'
  Add to your ~/.zshrc:
      fpath=("$HOME/.zsh/completions" $fpath)
      autoload -Uz compinit && compinit
HINT
    fi
}

install_bash() {
    ssr::ensure_dir "$(dirname "$BASH_DST")"
    ln -sfn "$BASH_SRC" "$BASH_DST"
    ssr::ok "bash: $BASH_DST"
    if ! command -v bash-completion >/dev/null 2>&1 \
       && [[ ! -f /opt/homebrew/etc/profile.d/bash_completion.sh \
          && ! -f /usr/local/etc/profile.d/bash_completion.sh ]]; then
        cat <<'HINT'
  Install bash-completion to activate:
      brew install bash-completion@2
  and add to ~/.bashrc:
      [[ -r /opt/homebrew/etc/profile.d/bash_completion.sh ]] && \
        source /opt/homebrew/etc/profile.d/bash_completion.sh
HINT
    fi
}

install_fish() {
    ssr::ensure_dir "$(dirname "$FISH_DST")"
    ln -sfn "$FISH_SRC" "$FISH_DST"
    ssr::ok "fish: $FISH_DST"
}

uninstall_zsh()  { rm -f "$ZSH_DST"  && ssr::ok "removed $ZSH_DST"; }
uninstall_bash() { rm -f "$BASH_DST" && ssr::ok "removed $BASH_DST"; }
uninstall_fish() { rm -f "$FISH_DST" && ssr::ok "removed $FISH_DST"; }

status_one() {
    local label="$1" dst="$2"
    if [[ -L "$dst" || -f "$dst" ]]; then
        printf '  %-5s installed: %s\n' "$label" "$dst"
    else
        printf '  %-5s not installed\n' "$label"
    fi
}

action="${1:-status}"; shift || true
shells=""
while [[ $# -gt 0 ]]; do
    case "$1" in
        --shell) shells="$2"; shift 2 ;;
        -h|--help) usage; exit 0 ;;
        *) ssr::die "Unknown flag: $1" ;;
    esac
done

if [[ -z "$shells" ]]; then
    shells="$(detect_shell)"
    [[ -n "$shells" ]] || ssr::die "Could not detect \$SHELL; pass --shell zsh|bash|fish|all"
fi
[[ "$shells" == "all" ]] && shells="zsh bash fish"

case "$action" in
    install)
        for s in $shells; do
            case "$s" in
                zsh)  install_zsh ;;
                bash) install_bash ;;
                fish) install_fish ;;
                *) ssr::die "Unknown shell: $s" ;;
            esac
        done
        ssr::log "Open a new shell (or 'exec \$SHELL') to activate."
        ;;
    uninstall)
        for s in $shells; do
            case "$s" in
                zsh)  uninstall_zsh ;;
                bash) uninstall_bash ;;
                fish) uninstall_fish ;;
                *) ssr::die "Unknown shell: $s" ;;
            esac
        done
        ;;
    status)
        echo "ssr completions:"
        status_one zsh  "$ZSH_DST"
        status_one bash "$BASH_DST"
        status_one fish "$FISH_DST"
        ;;
    *)
        usage; exit 1 ;;
esac
