import { Command } from 'commander';

const BASH_COMPLETION = `
# sentryagent bash completion
# Add to ~/.bashrc or ~/.bash_profile:
#   source <(sentryagent completion bash)

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

  local commands="configure register-agent list-agents issue-token rotate-credentials tail-audit-log completion"
  local global_opts="--help --version"

  case "\${prev}" in
    sentryagent)
      COMPREPLY=( \$(compgen -W "\${commands} \${global_opts}" -- "\${cur}") )
      return 0
      ;;
    configure)
      COMPREPLY=( \$(compgen -W "--help" -- "\${cur}") )
      return 0
      ;;
    register-agent)
      COMPREPLY=( \$(compgen -W "--name --description --help" -- "\${cur}") )
      return 0
      ;;
    list-agents)
      COMPREPLY=( \$(compgen -W "--help" -- "\${cur}") )
      return 0
      ;;
    issue-token)
      COMPREPLY=( \$(compgen -W "--agent-id --help" -- "\${cur}") )
      return 0
      ;;
    rotate-credentials)
      COMPREPLY=( \$(compgen -W "--agent-id --help" -- "\${cur}") )
      return 0
      ;;
    tail-audit-log)
      COMPREPLY=( \$(compgen -W "--agent-id --help" -- "\${cur}") )
      return 0
      ;;
    completion)
      COMPREPLY=( \$(compgen -W "bash zsh --help" -- "\${cur}") )
      return 0
      ;;
    *)
      COMPREPLY=()
      return 0
      ;;
  esac
}

complete -F _sentryagent_completion sentryagent
`.trim();

const ZSH_COMPLETION = `
#compdef sentryagent

# sentryagent zsh completion
# Add to ~/.zshrc:
#   source <(sentryagent completion zsh)
# Or generate a file and place it in your $fpath:
#   sentryagent completion zsh > ~/.zsh/completions/_sentryagent

_sentryagent() {
  local state

  _arguments \\
    '(-v --version)'{-v,--version}'[Show version]' \\
    '(-h --help)'{-h,--help}'[Show help]' \\
    '1: :->command' \\
    '*: :->args'

  case \$state in
    command)
      local commands=(
        'configure:Configure CLI with API URL and credentials'
        'register-agent:Register a new agent'
        'list-agents:List all registered agents'
        'issue-token:Issue an OAuth2 access token for an agent'
        'rotate-credentials:Rotate credentials for an agent'
        'tail-audit-log:Poll and stream audit log events'
        'completion:Output shell completion script'
      )
      _describe 'command' commands
      ;;
    args)
      case \${words[2]} in
        configure)
          _arguments \\
            '(-h --help)'{-h,--help}'[Show help]'
          ;;
        register-agent)
          _arguments \\
            '--name[Agent name]:name' \\
            '--description[Agent description]:description' \\
            '(-h --help)'{-h,--help}'[Show help]'
          ;;
        list-agents)
          _arguments \\
            '(-h --help)'{-h,--help}'[Show help]'
          ;;
        issue-token)
          _arguments \\
            '--agent-id[Agent ID]:agent-id' \\
            '(-h --help)'{-h,--help}'[Show help]'
          ;;
        rotate-credentials)
          _arguments \\
            '--agent-id[Agent ID]:agent-id' \\
            '(-h --help)'{-h,--help}'[Show help]'
          ;;
        tail-audit-log)
          _arguments \\
            '--agent-id[Filter by agent ID]:agent-id' \\
            '(-h --help)'{-h,--help}'[Show help]'
          ;;
        completion)
          local shells=('bash:Generate bash completion script' 'zsh:Generate zsh completion script')
          _describe 'shell' shells
          ;;
      esac
      ;;
  esac
}

_sentryagent "\$@"
`.trim();

export function registerCompletion(program: Command): void {
  const completion = program
    .command('completion')
    .description('Output shell completion scripts');

  completion
    .command('bash')
    .description('Output bash completion script')
    .action(() => {
      console.log(BASH_COMPLETION);
    });

  completion
    .command('zsh')
    .description('Output zsh completion script')
    .action(() => {
      console.log(ZSH_COMPLETION);
    });

  completion.addHelpText(
    'after',
    '\nSupported shells: bash, zsh',
  );
}
