#!/bin/bash
# Bash completion for netrinos
# Generated by: netrinos completion bash

_netrinos_completions() {
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Main commands
    local commands="? about accesscontrol agent alerts alerts-webhook check cloak configserver doctor exit find firewall fw gateway help id ida idp install licenses log login loglevel logout lport nat netconfig proxy restart retest routes run scan settings signal start status stop sync tunnels uiport uninstall update upgrade vdevices ver version versions wg wg1 wga wgf wgk wgport wgw"

    # Sub-commands for specific commands
    case "${prev}" in
        agent)
            COMPREPLY=( $(compgen -W "status start stop restart" -- ${cur}) )
            return 0
            ;;
        tunnels)
            COMPREPLY=( $(compgen -W "status start stop" -- ${cur}) )
            return 0
            ;;
        autostart)
            COMPREPLY=( $(compgen -W "yes no" -- ${cur}) )
            return 0
            ;;
        scan)
            COMPREPLY=( $(compgen -W "on off now" -- ${cur}) )
            return 0
            ;;
        routes)
            COMPREPLY=( $(compgen -W "add remove list clear gateway" -- ${cur}) )
            return 0
            ;;
        vdevices)
            COMPREPLY=( $(compgen -W "list scan" -- ${cur}) )
            return 0
            ;;
        vdevice)
            COMPREPLY=( $(compgen -W "add del" -- ${cur}) )
            return 0
            ;;
        accesscontrol|acl)
            COMPREPLY=( $(compgen -W "list show status enable disable set add remove reset clear expanded ip" -- ${cur}) )
            return 0
            ;;
        upgrade)
            if [[ ${COMP_CWORD} -eq 2 ]]; then
                COMPREPLY=( $(compgen -W "channel latest daily beta" -- ${cur}) )
            elif [[ ${COMP_CWORD} -eq 3 ]] && [[ ${prev} == "channel" ]]; then
                COMPREPLY=( $(compgen -W "latest daily beta" -- ${cur}) )
            fi
            return 0
            ;;
        loglevel|ll)
            COMPREPLY=( $(compgen -W "debug info warning error" -- ${cur}) )
            return 0
            ;;
        lport|wgport|uiport)
            COMPREPLY=( $(compgen -W "auto" -- ${cur}) )
            return 0
            ;;
        completion)
            COMPREPLY=( $(compgen -W "bash zsh install" -- ${cur}) )
            return 0
            ;;
    esac

    # If we're at the first argument, complete with main commands
    if [[ ${COMP_CWORD} -eq 1 ]]; then
        COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
        return 0
    fi

    # Handle routes sub-sub-commands
    if [[ ${COMP_WORDS[1]} == "routes" ]]; then
        case "${COMP_WORDS[2]}" in
            add)
                # Could complete with common destinations or gateways
                if [[ ${COMP_CWORD} -eq 3 ]]; then
                    # Destination suggestions
                    COMPREPLY=( $(compgen -W "ifconfig.co google.com" -- ${cur}) )
                elif [[ ${COMP_CWORD} -eq 4 ]]; then
                    # Gateway suggestions - could parse from 'netrinos wg' output
                    local gateways=$(netrinos wg 2>/dev/null | grep -oE '^[a-zA-Z0-9.-]+\.[a-zA-Z0-9]+' | head -5)
                    COMPREPLY=( $(compgen -W "${gateways}" -- ${cur}) )
                fi
                return 0
                ;;
            remove)
                # Complete with existing routes
                local routes=$(netrinos routes list 2>/dev/null | grep -oE '^[a-zA-Z0-9.-]+' | head -10)
                COMPREPLY=( $(compgen -W "${routes}" -- ${cur}) )
                return 0
                ;;
        esac
    fi
}

# Register the completion function
complete -F _netrinos_completions netrinos
