# Bash completion for SecretHub CLI
# Generated by SecretHub.CLI.Completion

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

    # Global options
    local global_opts="--help --version --server --format --quiet --verbose -h -v -s -f -q"
    local format_opts="json table yaml"

    # Main commands
    local commands="login logout whoami secret policy agent config completion help version"

    # Subcommands
    local secret_commands="list get create update delete versions rollback"
    local policy_commands="list get create update delete simulate templates"
    local agent_commands="list status logs"
    local config_commands="list get set"

    # Handle format option completion
    if [[ "${prev}" == "--format" ]] || [[ "${prev}" == "-f" ]]; then
        COMPREPLY=( $(compgen -W "${format_opts}" -- ${cur}) )
        return 0
    fi

    # Handle server option completion
    if [[ "${prev}" == "--server" ]] || [[ "${prev}" == "-s" ]]; then
        # No completion for server URL
        return 0
    fi

    # Handle login options
    if [[ "${prev}" == "--role-id" ]] || [[ "${prev}" == "--secret-id" ]] || \
       [[ "${prev}" == "--value" ]] || [[ "${prev}" == "--name" ]]; then
        # No completion for value inputs
        return 0
    fi

    # Handle from-template option
    if [[ "${prev}" == "--from-template" ]]; then
        local templates="$(_secrethub_get_policy_templates)"
        COMPREPLY=( $(compgen -W "${templates}" -- ${cur}) )
        return 0
    fi

    # Determine context based on command words
    local cmd="${COMP_WORDS[1]}"
    local subcmd="${COMP_WORDS[2]}"

    # Completion for completion command
    if [[ "${cmd}" == "completion" ]]; then
        if [[ ${COMP_CWORD} -eq 2 ]]; then
            COMPREPLY=( $(compgen -W "bash zsh" -- ${cur}) )
        fi
        return 0
    fi

    # Completion for secret commands
    if [[ "${cmd}" == "secret" ]]; then
        if [[ ${COMP_CWORD} -eq 2 ]]; then
            COMPREPLY=( $(compgen -W "${secret_commands}" -- ${cur}) )
            return 0
        elif [[ ${COMP_CWORD} -eq 3 ]]; then
            case "${subcmd}" in
                get|update|delete|versions|rollback)
                    local secrets="$(_secrethub_get_secrets)"
                    COMPREPLY=( $(compgen -W "${secrets}" -- ${cur}) )
                    return 0
                    ;;
                create)
                    # No completion for new secret path
                    return 0
                    ;;
                list)
                    COMPREPLY=( $(compgen -W "${global_opts}" -- ${cur}) )
                    return 0
                    ;;
            esac
        elif [[ ${COMP_CWORD} -gt 3 ]]; then
            # Complete options for secret commands
            local secret_opts="--value --from-file ${global_opts}"
            COMPREPLY=( $(compgen -W "${secret_opts}" -- ${cur}) )
            return 0
        fi
    fi

    # Completion for policy commands
    if [[ "${cmd}" == "policy" ]]; then
        if [[ ${COMP_CWORD} -eq 2 ]]; then
            COMPREPLY=( $(compgen -W "${policy_commands}" -- ${cur}) )
            return 0
        elif [[ ${COMP_CWORD} -eq 3 ]]; then
            case "${subcmd}" in
                get|update|delete|simulate)
                    local policies="$(_secrethub_get_policies)"
                    COMPREPLY=( $(compgen -W "${policies}" -- ${cur}) )
                    return 0
                    ;;
                create)
                    local policy_opts="--from-template --name ${global_opts}"
                    COMPREPLY=( $(compgen -W "${policy_opts}" -- ${cur}) )
                    return 0
                    ;;
                list|templates)
                    COMPREPLY=( $(compgen -W "${global_opts}" -- ${cur}) )
                    return 0
                    ;;
            esac
        elif [[ ${COMP_CWORD} -gt 3 ]]; then
            # Complete options for policy commands
            local policy_opts="--from-template --name ${global_opts}"
            COMPREPLY=( $(compgen -W "${policy_opts}" -- ${cur}) )
            return 0
        fi
    fi

    # Completion for agent commands
    if [[ "${cmd}" == "agent" ]]; then
        if [[ ${COMP_CWORD} -eq 2 ]]; then
            COMPREPLY=( $(compgen -W "${agent_commands}" -- ${cur}) )
            return 0
        elif [[ ${COMP_CWORD} -eq 3 ]]; then
            case "${subcmd}" in
                status|logs)
                    local agents="$(_secrethub_get_agents)"
                    COMPREPLY=( $(compgen -W "${agents}" -- ${cur}) )
                    return 0
                    ;;
                list)
                    COMPREPLY=( $(compgen -W "${global_opts}" -- ${cur}) )
                    return 0
                    ;;
            esac
        elif [[ ${COMP_CWORD} -gt 3 ]]; then
            # Complete options for agent commands
            COMPREPLY=( $(compgen -W "${global_opts}" -- ${cur}) )
            return 0
        fi
    fi

    # Completion for config commands
    if [[ "${cmd}" == "config" ]]; then
        if [[ ${COMP_CWORD} -eq 2 ]]; then
            COMPREPLY=( $(compgen -W "${config_commands}" -- ${cur}) )
            return 0
        elif [[ ${COMP_CWORD} -eq 3 ]]; then
            case "${subcmd}" in
                get|set)
                    local config_keys="server_url default_format log_level"
                    COMPREPLY=( $(compgen -W "${config_keys}" -- ${cur}) )
                    return 0
                    ;;
                list)
                    COMPREPLY=( $(compgen -W "${global_opts}" -- ${cur}) )
                    return 0
                    ;;
            esac
        elif [[ ${COMP_CWORD} -eq 4 ]]; then
            case "${subcmd}" in
                set)
                    # Complete value for config set
                    local key="${COMP_WORDS[3]}"
                    if [[ "${key}" == "default_format" ]]; then
                        COMPREPLY=( $(compgen -W "${format_opts}" -- ${cur}) )
                    fi
                    return 0
                    ;;
            esac
        fi
    fi

    # Completion for login command
    if [[ "${cmd}" == "login" ]]; then
        local login_opts="--role-id --secret-id ${global_opts}"
        COMPREPLY=( $(compgen -W "${login_opts}" -- ${cur}) )
        return 0
    fi

    # Completion for logout and whoami (only global options)
    if [[ "${cmd}" == "logout" ]] || [[ "${cmd}" == "whoami" ]]; then
        COMPREPLY=( $(compgen -W "${global_opts}" -- ${cur}) )
        return 0
    fi

    # Main command completion
    if [[ ${COMP_CWORD} -eq 1 ]]; then
        COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
        return 0
    fi

    # Default to global options
    COMPREPLY=( $(compgen -W "${global_opts}" -- ${cur}) )
    return 0
}

# Helper function to get secrets from server
_secrethub_get_secrets() {
    local config_file="${HOME}/.secrethub/config.toml"
    if [[ -f "${config_file}" ]]; then
        # Try to get secrets from server (with timeout)
        secrethub secret list --format json 2>/dev/null | \
            grep -o '"path":"[^"]*"' | \
            cut -d'"' -f4 2>/dev/null || echo ""
    fi
}

# Helper function to get policies from server
_secrethub_get_policies() {
    local config_file="${HOME}/.secrethub/config.toml"
    if [[ -f "${config_file}" ]]; then
        # Try to get policies from server (with timeout)
        secrethub policy list --format json 2>/dev/null | \
            grep -o '"name":"[^"]*"' | \
            cut -d'"' -f4 2>/dev/null || echo ""
    fi
}

# Helper function to get agents from server
_secrethub_get_agents() {
    local config_file="${HOME}/.secrethub/config.toml"
    if [[ -f "${config_file}" ]]; then
        # Try to get agents from server (with timeout)
        secrethub agent list --format json 2>/dev/null | \
            grep -o '"id":"[^"]*"' | \
            cut -d'"' -f4 2>/dev/null || echo ""
    fi
}

# Helper function to get policy templates
_secrethub_get_policy_templates() {
    echo "admin read_only business_hours time_limited emergency_access"
}

complete -F _secrethub secrethub
