# Bash completion for skbs
# Generated by Cyclopts (app.generate_completion(prog_name='skbs', shell='bash'))
# then hand-patched: the "gen"/"g" TEMPLATE positional (positional_count==0)
# calls `skbs _complete-templates` for a live list, since cyclopts can only
# generate static completions. If skbs's commands/options change, regenerate
# with the above call and re-apply that one patch (search for
# "_complete-templates" below).

_skbs() {
  local cur prev

  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"

  # Build list of options that take values (to skip their arguments)
  local options_with_values='--dest --name --path --src --template -n'

  # Build list of all valid command names (to distinguish from positionals)
  local all_commands='config-path create-config g gen i i-d install install-defaults l list ls u uninstall'

  # Detect command path by collecting valid command words only
  local -a cmd_path=()
  local i skip_next=0
  for ((i=1; i<COMP_CWORD; i++)); do
    local word="${COMP_WORDS[i]}"
    if [[ $skip_next -eq 1 ]]; then
      skip_next=0
      continue
    fi
    if [[ $word =~ ^- ]]; then
      # Check if this option takes a value
      if [[ " $options_with_values " =~ " $word " ]]; then
        skip_next=1
      fi
    else
      # Non-option word - only add to cmd_path if it's a valid command
      if [[ " $all_commands " =~ " $word " ]]; then
        cmd_path+=("$word")
      fi
    fi
  done

  # Count positionals (non-option words after command path)
  local positional_count=0
  local cmd_path_len=${#cmd_path[@]}
  skip_next=0
  local cmd_depth=0
  for ((i=1; i<COMP_CWORD; i++)); do
    local word="${COMP_WORDS[i]}"
    if [[ $skip_next -eq 1 ]]; then
      skip_next=0
      continue
    fi
    if [[ $word =~ ^- ]]; then
      if [[ " $options_with_values " =~ " $word " ]]; then
        skip_next=1
      fi
    else
      # Non-option word
      if [[ $cmd_depth -lt $cmd_path_len ]]; then
        # Still in command path
        ((cmd_depth++))
      else
        # Past command path, this is a positional
        ((positional_count++))
      fi
    fi
  done

  # Determine command level and generate completions
  case "${#cmd_path[@]}" in
    0)
      if [[ ${cur} == -* ]]; then
        local -a _c=("--help" "-h" "--version")
        COMPREPLY=()
        for _x in "${_c[@]}"; do
          [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
        done
      else
        local -a _c=("create-config" "config-path" "install-defaults" "i-d" "install" "i" "uninstall" "u" "list" "l" "ls" "gen" "g")
        COMPREPLY=()
        for _x in "${_c[@]}"; do
          [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
        done
      fi
      ;;
    1)
      case "${cmd_path[@]}" in
        "config-path")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            COMPREPLY=()
          fi
          ;;
        "create-config")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--path" "--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            local _value_prev="${prev}"
            if [[ "$_value_prev" == "=" && $COMP_CWORD -ge 2 ]]; then
              _value_prev="${COMP_WORDS[COMP_CWORD-2]}"
            fi
            case "$_value_prev" in
              --path)
                COMPREPLY=()
                ;;
              *)
                COMPREPLY=()
                ;;
            esac
          fi
          ;;
        "g")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--template" "--dest" "--debug" "-g" "--no-debug" "--stdout" "--no-stdout" "--single-file" "-s" "--no-single-file" "--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            local _value_prev="${prev}"
            if [[ "$_value_prev" == "=" && $COMP_CWORD -ge 2 ]]; then
              _value_prev="${COMP_WORDS[COMP_CWORD-2]}"
            fi
            case "$_value_prev" in
              --template)
                COMPREPLY=()
                ;;
              --dest)
                COMPREPLY=()
                ;;
              *)
                case ${positional_count} in
                  0)
                    # Dynamic: list installed/local templates for TEMPLATE (cyclopts can't know these statically)
                    COMPREPLY=( $(compgen -W "$("${COMP_WORDS[0]}" _complete-templates "$cur" 2>/dev/null)" -- "$cur") )
                    compopt -o nospace  # template names can be paths (e.g. "cpp/class") - let the user keep tabbing into subdirectories
                    ;;
                  1)
                    COMPREPLY=()
                    ;;
                  *)
                    COMPREPLY=()
                    ;;
                esac
                ;;
            esac
          fi
          ;;
        "gen")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--template" "--dest" "--debug" "-g" "--no-debug" "--stdout" "--no-stdout" "--single-file" "-s" "--no-single-file" "--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            local _value_prev="${prev}"
            if [[ "$_value_prev" == "=" && $COMP_CWORD -ge 2 ]]; then
              _value_prev="${COMP_WORDS[COMP_CWORD-2]}"
            fi
            case "$_value_prev" in
              --template)
                COMPREPLY=()
                ;;
              --dest)
                COMPREPLY=()
                ;;
              *)
                case ${positional_count} in
                  0)
                    # Dynamic: list installed/local templates for TEMPLATE (cyclopts can't know these statically)
                    COMPREPLY=( $(compgen -W "$("${COMP_WORDS[0]}" _complete-templates "$cur" 2>/dev/null)" -- "$cur") )
                    compopt -o nospace  # template names can be paths (e.g. "cpp/class") - let the user keep tabbing into subdirectories
                    ;;
                  1)
                    COMPREPLY=()
                    ;;
                  *)
                    COMPREPLY=()
                    ;;
                esac
                ;;
            esac
          fi
          ;;
        "i")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--src" "--symlink" "-s" "--no-symlink" "--name" "-n" "--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            local _value_prev="${prev}"
            if [[ "$_value_prev" == "=" && $COMP_CWORD -ge 2 ]]; then
              _value_prev="${COMP_WORDS[COMP_CWORD-2]}"
            fi
            case "$_value_prev" in
              --src)
                COMPREPLY=()
                ;;
              --name)
                COMPREPLY=()
                ;;
              -n)
                COMPREPLY=()
                ;;
              *)
                case ${positional_count} in
                  0)
                    COMPREPLY=()
                    ;;
                  1)
                    COMPREPLY=()
                    ;;
                  2)
                    COMPREPLY=()
                    ;;
                  *)
                    COMPREPLY=()
                    ;;
                esac
                ;;
            esac
          fi
          ;;
        "i-d")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--symlink" "-s" "--no-symlink" "--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            COMPREPLY=()
          fi
          ;;
        "install")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--src" "--symlink" "-s" "--no-symlink" "--name" "-n" "--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            local _value_prev="${prev}"
            if [[ "$_value_prev" == "=" && $COMP_CWORD -ge 2 ]]; then
              _value_prev="${COMP_WORDS[COMP_CWORD-2]}"
            fi
            case "$_value_prev" in
              --src)
                COMPREPLY=()
                ;;
              --name)
                COMPREPLY=()
                ;;
              -n)
                COMPREPLY=()
                ;;
              *)
                case ${positional_count} in
                  0)
                    COMPREPLY=()
                    ;;
                  1)
                    COMPREPLY=()
                    ;;
                  2)
                    COMPREPLY=()
                    ;;
                  *)
                    COMPREPLY=()
                    ;;
                esac
                ;;
            esac
          fi
          ;;
        "install-defaults")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--symlink" "-s" "--no-symlink" "--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            COMPREPLY=()
          fi
          ;;
        "l")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            COMPREPLY=()
          fi
          ;;
        "list")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            COMPREPLY=()
          fi
          ;;
        "ls")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            COMPREPLY=()
          fi
          ;;
        "u")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--name" "--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            local _value_prev="${prev}"
            if [[ "$_value_prev" == "=" && $COMP_CWORD -ge 2 ]]; then
              _value_prev="${COMP_WORDS[COMP_CWORD-2]}"
            fi
            case "$_value_prev" in
              --name)
                COMPREPLY=()
                ;;
              *)
                COMPREPLY=()
                ;;
            esac
          fi
          ;;
        "uninstall")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--name" "--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            local _value_prev="${prev}"
            if [[ "$_value_prev" == "=" && $COMP_CWORD -ge 2 ]]; then
              _value_prev="${COMP_WORDS[COMP_CWORD-2]}"
            fi
            case "$_value_prev" in
              --name)
                COMPREPLY=()
                ;;
              *)
                COMPREPLY=()
                ;;
            esac
          fi
          ;;
        *)
          ;;
      esac
      ;;
    *)
      ;;
  esac
}

complete -F _skbs skbs
