# Bash completion script for OpenSpec CLI
# Auto-generated - do not edit manually

_openspec_completion() {
  local cur prev words cword

  # Use _init_completion if available (from bash-completion package)
  # The -n : option prevents colons from being treated as word separators
  # (important for spec/change IDs that may contain colons)
  # Otherwise, fall back to manual initialization
  if declare -F _init_completion >/dev/null 2>&1; then
    _init_completion -n : || return
  else
    # Manual fallback when bash-completion is not installed
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    words=("${COMP_WORDS[@]}")
    cword=$COMP_CWORD
  fi

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

  # Top-level commands
  if [[ $cword -eq 1 ]]; then
    local commands="init update list view validate show archive feedback change spec completion config schema"
    COMPREPLY=($(compgen -W "$commands" -- "$cur"))
    return 0
  fi

  # Command-specific completion
  case "$cmd" in
    init)
      if [[ "$cur" == -* ]]; then
        local flags="--tools"
        COMPREPLY=($(compgen -W "$flags" -- "$cur"))
        return 0
      fi

      COMPREPLY=($(compgen -f -- "$cur"))
      ;;
    update)
      COMPREPLY=($(compgen -f -- "$cur"))
      ;;
    list)
      if [[ "$cur" == -* ]]; then
        local flags="--specs --changes"
        COMPREPLY=($(compgen -W "$flags" -- "$cur"))
        return 0
      fi

      ;;
    view)
      ;;
    validate)
      if [[ "$cur" == -* ]]; then
        local flags="--all --changes --specs --type --strict --json --concurrency --no-interactive"
        COMPREPLY=($(compgen -W "$flags" -- "$cur"))
        return 0
      fi

      _openspec_complete_items
      ;;
    show)
      if [[ "$cur" == -* ]]; then
        local flags="--json --type --no-interactive --deltas-only --requirements-only --requirements --no-scenarios -r --requirement"
        COMPREPLY=($(compgen -W "$flags" -- "$cur"))
        return 0
      fi

      _openspec_complete_items
      ;;
    archive)
      if [[ "$cur" == -* ]]; then
        local flags="-y --yes --skip-specs --no-validate"
        COMPREPLY=($(compgen -W "$flags" -- "$cur"))
        return 0
      fi

      _openspec_complete_changes
      ;;
    feedback)
      if [[ "$cur" == -* ]]; then
        local flags="--body"
        COMPREPLY=($(compgen -W "$flags" -- "$cur"))
        return 0
      fi

      ;;
    change)
      if [[ $cword -eq 2 ]]; then
        local subcommands="show list validate"
        COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
        return 0
      fi

      case "$subcmd" in
        show)
          if [[ "$cur" == -* ]]; then
            local flags="--json --deltas-only --requirements-only --no-interactive"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          _openspec_complete_changes
          ;;
        list)
          if [[ "$cur" == -* ]]; then
            local flags="--json --long"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          ;;
        validate)
          if [[ "$cur" == -* ]]; then
            local flags="--strict --json --no-interactive"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          _openspec_complete_changes
          ;;
      esac
      ;;
    spec)
      if [[ $cword -eq 2 ]]; then
        local subcommands="show list validate"
        COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
        return 0
      fi

      case "$subcmd" in
        show)
          if [[ "$cur" == -* ]]; then
            local flags="--json --requirements --no-scenarios -r --requirement --no-interactive"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          _openspec_complete_specs
          ;;
        list)
          if [[ "$cur" == -* ]]; then
            local flags="--json --long"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          ;;
        validate)
          if [[ "$cur" == -* ]]; then
            local flags="--strict --json --no-interactive"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          _openspec_complete_specs
          ;;
      esac
      ;;
    completion)
      if [[ $cword -eq 2 ]]; then
        local subcommands="generate install uninstall"
        COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
        return 0
      fi

      case "$subcmd" in
        generate)
          local shells="zsh bash fish powershell"
          COMPREPLY=($(compgen -W "$shells" -- "$cur"))
          ;;
        install)
          if [[ "$cur" == -* ]]; then
            local flags="--verbose"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          local shells="zsh bash fish powershell"
          COMPREPLY=($(compgen -W "$shells" -- "$cur"))
          ;;
        uninstall)
          if [[ "$cur" == -* ]]; then
            local flags="-y --yes"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          local shells="zsh bash fish powershell"
          COMPREPLY=($(compgen -W "$shells" -- "$cur"))
          ;;
      esac
      ;;
    config)
      if [[ "$cur" == -* ]]; then
        local flags="--scope"
        COMPREPLY=($(compgen -W "$flags" -- "$cur"))
        return 0
      fi

      if [[ $cword -eq 2 ]]; then
        local subcommands="path list get set unset reset edit profile"
        COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
        return 0
      fi

      case "$subcmd" in
        path)
          ;;
        list)
          if [[ "$cur" == -* ]]; then
            local flags="--json"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          ;;
        get)
          ;;
        set)
          if [[ "$cur" == -* ]]; then
            local flags="--string --allow-unknown"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          ;;
        unset)
          ;;
        reset)
          if [[ "$cur" == -* ]]; then
            local flags="--all -y --yes"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          ;;
        edit)
          ;;
        profile)
          ;;
      esac
      ;;
    schema)
      if [[ $cword -eq 2 ]]; then
        local subcommands="which validate fork init"
        COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
        return 0
      fi

      case "$subcmd" in
        which)
          if [[ "$cur" == -* ]]; then
            local flags="--json --all"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          ;;
        validate)
          if [[ "$cur" == -* ]]; then
            local flags="--json --verbose"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          ;;
        fork)
          if [[ "$cur" == -* ]]; then
            local flags="--json --force"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          ;;
        init)
          if [[ "$cur" == -* ]]; then
            local flags="--json --description --artifacts --default --no-default --force"
            COMPREPLY=($(compgen -W "$flags" -- "$cur"))
            return 0
          fi

          ;;
      esac
      ;;
  esac

  return 0
}

# Dynamic completion helpers

_openspec_complete_changes() {
  local changes
  changes=$(openspec __complete changes 2>/dev/null | cut -f1)
  COMPREPLY=($(compgen -W "$changes" -- "$cur"))
}

_openspec_complete_specs() {
  local specs
  specs=$(openspec __complete specs 2>/dev/null | cut -f1)
  COMPREPLY=($(compgen -W "$specs" -- "$cur"))
}

_openspec_complete_items() {
  local items
  items=$(openspec __complete changes 2>/dev/null | cut -f1; openspec __complete specs 2>/dev/null | cut -f1)
  COMPREPLY=($(compgen -W "$items" -- "$cur"))
}
complete -F _openspec_completion openspec

