# SPDX-FileCopyrightText: 2026 Damy Metzke
# SPDX-License-Identifier: GPL-3.0-or-later

_repo() {
  local cur prev words cword
  if ! _init_completion -n :; then
    return
  fi

  if ! command -v repo >/dev/null 2>&1; then
    return
  fi

  local subcmd="${words[1]}"

  _repo_groups() {
    command repo completion groups 2>/dev/null
  }

  _repo_repos() {
    local group="$1"
    if [[ -z "$group" ]]; then
      return
    fi
    command repo completion repos --group "$group" 2>/dev/null
  }

  if [[ $cword -eq 1 ]]; then
    COMPREPLY=( $(compgen -W "groups repos git tmux nix" -- "$cur") )
    return
  fi

  case "$subcmd" in
    groups)
      if [[ "$prev" == "--output" ]]; then
        COMPREPLY=( $(compgen -W "human plain" -- "$cur") )
        return
      fi
      COMPREPLY=( $(compgen -W "--output --tagged --no-tagged" -- "$cur") )
      ;;
    repos)
      if [[ "$prev" == "--group" ]]; then
        local groups
        groups="$(_repo_groups)"
        COMPREPLY=( $(compgen -W "$groups" -- "$cur") )
        return
      fi
      if [[ "$prev" == "--format" ]]; then
        COMPREPLY=( $(compgen -W "name group path" -- "$cur") )
        return
      fi
      if [[ "$prev" == "--output" ]]; then
        COMPREPLY=( $(compgen -W "human plain" -- "$cur") )
        return
      fi
      COMPREPLY=( $(compgen -W "--group --format --output --tagged --no-tagged" -- "$cur") )
      ;;
    git)
      local git_cmd="${words[2]}"
      if [[ $cword -eq 2 ]]; then
        COMPREPLY=( $(compgen -W "status sync" -- "$cur") )
        return
      fi
      case "$git_cmd" in
        status)
          if [[ "$prev" == "--group" ]]; then
            local groups
            groups="$(_repo_groups)"
            COMPREPLY=( $(compgen -W "$groups" -- "$cur") )
            return
          fi
          if [[ "$prev" == "--format" ]]; then
            COMPREPLY=( $(compgen -W "name group path" -- "$cur") )
            return
          fi
          if [[ "$prev" == "--output" ]]; then
            COMPREPLY=( $(compgen -W "human plain" -- "$cur") )
            return
          fi
          COMPREPLY=( $(compgen -W "--group --format --output --fetch --detailed" -- "$cur") )
          ;;
        sync)
          if [[ "$prev" == "--group" ]]; then
            local groups
            groups="$(_repo_groups)"
            COMPREPLY=( $(compgen -W "$groups" -- "$cur") )
            return
          fi
          if [[ "$prev" == "--format" ]]; then
            COMPREPLY=( $(compgen -W "name group path" -- "$cur") )
            return
          fi
          COMPREPLY=( $(compgen -W "--group --format --dry-run" -- "$cur") )
          ;;
      esac
      ;;
    tmux)
      local tmux_cmd="${words[2]}"
      if [[ $cword -eq 2 ]]; then
        COMPREPLY=( $(compgen -W "init start path" -- "$cur") )
        return
      fi
      case "$tmux_cmd" in
        start)
          if [[ $cword -eq 3 ]]; then
            local groups
            groups="$(_repo_groups)"
            COMPREPLY=( $(compgen -W "$groups" -- "$cur") )
            return
          fi
          if [[ $cword -eq 4 ]]; then
            local group="${words[3]}"
            local repos
            repos="$(_repo_repos "$group")"
            COMPREPLY=( $(compgen -W "$repos" -- "$cur") )
            return
          fi
          ;;
        path)
          ;;
      esac
      ;;
    nix)
      local nix_cmd="${words[2]}"
      if [[ $cword -eq 2 ]]; then
        COMPREPLY=( $(compgen -W "summarize-sources update-sources" -- "$cur") )
        return
      fi
      case "$nix_cmd" in
        summarize-sources)
          if [[ "$prev" == "--output" ]]; then
            COMPREPLY=( $(compgen -W "human plain" -- "$cur") )
            return
          fi
          COMPREPLY=( $(compgen -W "--output --revisions" -- "$cur") )
          ;;
        update-sources)
          COMPREPLY=( $(compgen -W "--all" -- "$cur") )
          ;;
      esac
      ;;
  esac
}

complete -F _repo repo
