# AINEOS CLI bash completion
# Source this file: source /path/to/aineos
# Or install: aineos completion bash > /usr/local/etc/bash_completion.d/aineos

_aineos_completions() {
  local cur prev words cword
  _init_completion || return

  # Global options
  local global_opts="--help --version --json -v -w --workspace"

  # Commands with their options
  local commands="list read write diff init config check audit status propagate verify create predict heal notify run auto hooks completion"

  # Subcommand-specific args
  case $prev in
    aineos)
      COMPREPLY=($(compgen -W "$commands $global_opts" -- "$cur"))
      return
      ;;
    list)
      COMPREPLY=($(compgen -W "--type --status --json -w --workspace" -- "$cur"))
      return
      ;;
    read)
      COMPREPLY=($(compgen -W "--json -w --workspace" -- "$cur"))
      # Could add artifact path completion here
      return
      ;;
    write)
      COMPREPLY=($(compgen -W "--stdin --json -w --workspace" -- "$cur"))
      return
      ;;
    diff)
      COMPREPLY=($(compgen -W "--json -w --workspace" -- "$cur"))
      return
      ;;
    init)
      COMPREPLY=($(compgen -W "--force --template --with-git-hooks" -- "$cur"))
      return
      ;;
    config)
      COMPREPLY=($(compgen -W "get set list" -- "$cur"))
      return
      ;;
    check)
      COMPREPLY=($(compgen -W "--rules --fix --format --output --judge --judge-mock --judge-rules" -- "$cur"))
      return
      ;;
    audit)
      COMPREPLY=($(compgen -W "query stats history recent --userId --action --resource --limit --json" -- "$cur"))
      return
      ;;
    status)
      COMPREPLY=($(compgen -W "--json -w --workspace" -- "$cur"))
      return
      ;;
    propagate)
      COMPREPLY=($(compgen -W "--format --deep --json -w --workspace" -- "$cur"))
      return
      ;;
    verify)
      COMPREPLY=($(compgen -W "ui-spec design tests report --format --output --dry-run --json -w --workspace" -- "$cur"))
      return
      ;;
    create)
      COMPREPLY=($(compgen -W "BRD API-SPEC SCHEMA DESIGN UI-SPEC DOCUMENT TEST-PLAN TEST-CASE --list --title --desc --author --resource --resource-cn" -- "$cur"))
      return
      ;;
    predict)
      COMPREPLY=($(compgen -W "--risk-threshold --list-risks --watch --watch-interval --format --output" -- "$cur"))
      return
      ;;
    heal)
      COMPREPLY=($(compgen -W "--dry-run --apply --mode --rules --no-propagate --no-audit --format --output" -- "$cur"))
      return
      ;;
    notify)
      COMPREPLY=($(compgen -W "list setup ui test remove wechat dingtalk slack --webhook --name --port" -- "$cur"))
      return
      ;;
    run)
      COMPREPLY=($(compgen -W "list info pipeline --category --rules --path --type --content --json" -- "$cur"))
      return
      ;;
    auto)
      COMPREPLY=($(compgen -W "--dry-run --apply --mode --risk-threshold --watch --no-propagate --no-audit --deep --format --output" -- "$cur"))
      return
      ;;
    completion)
      COMPREPLY=($(compgen -W "bash zsh" -- "$cur"))
      return
      ;;
    hooks)
      COMPREPLY=($(compgen -W "install uninstall status" -- "$cur"))
      return
      ;;
  esac

  # If previous word is a known option that takes a value, don't suggest commands
  case $prev in
    --type|--status|--rules|--format|--output|--mode|--risk-threshold|--watch|--watch-interval|--template|--action|--resource|--userId|--title|--desc|--author|--name|--port|--webhook|--category|--path|--content|--judge-rules|-w|--workspace)
      return
      ;;
  esac

  # Default: complete with commands and flags
  if [[ "$cur" == --* ]]; then
    COMPREPLY=($(compgen -W "$global_opts" -- "$cur"))
  elif [[ "$cur" == -* ]]; then
    COMPREPLY=($(compgen -W "$global_opts" -- "$cur"))
  else
    COMPREPLY=($(compgen -W "$commands" -- "$cur"))
  fi
}

complete -F _aineos_completions aineos
