#compdef secrethub
# Zsh completion for SecretHub CLI
# Generated by SecretHub.CLI.Completion

_secrethub() {
    local curcontext="$curcontext" state line
    typeset -A opt_args

    local -a global_options
    global_options=(
        '(-h --help)'{-h,--help}'[Show help message]'
        '(-v --version)'{-v,--version}'[Show version]'
        '(-s --server)'{-s,--server}'[Server URL]:url:_urls'
        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)'
        '(-q --quiet)'{-q,--quiet}'[Suppress output]'
        '--verbose[Detailed output]'
    )

    _arguments -C \
        '1: :->command' \
        '*::arg:->args' \
        $global_options

    case $state in
        command)
            local -a commands
            commands=(
                'login:Authenticate with SecretHub server'
                'logout:Clear authentication credentials'
                'whoami:Show current authentication status'
                'secret:Manage secrets'
                'policy:Manage access policies'
                'agent:Manage SecretHub agents'
                'config:Manage CLI configuration'
                'completion:Generate shell completion scripts'
                'help:Show help message'
                'version:Show version information'
            )
            _describe 'secrethub commands' commands
            ;;

        args)
            case $line[1] in
                login)
                    _arguments \
                        '--role-id[AppRole role ID]:role_id:' \
                        '--secret-id[AppRole secret ID]:secret_id:' \
                        $global_options
                    ;;

                logout|whoami)
                    _arguments $global_options
                    ;;

                secret)
                    _secrethub_secret_commands
                    ;;

                policy)
                    _secrethub_policy_commands
                    ;;

                agent)
                    _secrethub_agent_commands
                    ;;

                config)
                    _secrethub_config_commands
                    ;;

                completion)
                    _arguments \
                        '1:shell:(bash zsh)' \
                        $global_options
                    ;;
            esac
            ;;
    esac
}

_secrethub_secret_commands() {
    local curcontext="$curcontext" state line
    typeset -A opt_args

    local -a secret_commands
    secret_commands=(
        'list:List all secrets'
        'get:Get a secret value'
        'create:Create a new secret'
        'update:Update an existing secret'
        'delete:Delete a secret'
        'versions:Show version history'
        'rollback:Rollback to a previous version'
    )

    _arguments -C \
        '1: :->subcmd' \
        '*::arg:->args'

    case $state in
        subcmd)
            _describe 'secret commands' secret_commands
            ;;

        args)
            case $line[1] in
                list)
                    _arguments \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;

                get|update|delete|versions)
                    _arguments \
                        '1:path:_secrethub_complete_secrets' \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;

                create)
                    _arguments \
                        '1:path:' \
                        '--value[Secret value]:value:' \
                        '--from-file[Read value from file]:file:_files' \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;

                rollback)
                    _arguments \
                        '1:path:_secrethub_complete_secrets' \
                        '2:version:' \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;
            esac
            ;;
    esac
}

_secrethub_policy_commands() {
    local curcontext="$curcontext" state line
    typeset -A opt_args

    local -a policy_commands
    policy_commands=(
        'list:List all policies'
        'get:Get policy details'
        'create:Create a new policy'
        'update:Update a policy'
        'delete:Delete a policy'
        'simulate:Simulate policy evaluation'
        'templates:List available policy templates'
    )

    _arguments -C \
        '1: :->subcmd' \
        '*::arg:->args'

    case $state in
        subcmd)
            _describe 'policy commands' policy_commands
            ;;

        args)
            case $line[1] in
                list|templates)
                    _arguments \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;

                get|delete|simulate)
                    _arguments \
                        '1:name:_secrethub_complete_policies' \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;

                create)
                    _arguments \
                        '--from-template[Use policy template]:template:_secrethub_complete_policy_templates' \
                        '--name[Policy name]:name:' \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;

                update)
                    _arguments \
                        '1:name:_secrethub_complete_policies' \
                        '--from-template[Use policy template]:template:_secrethub_complete_policy_templates' \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;
            esac
            ;;
    esac
}

_secrethub_agent_commands() {
    local curcontext="$curcontext" state line
    typeset -A opt_args

    local -a agent_commands
    agent_commands=(
        'list:List connected agents'
        'status:Get agent status'
        'logs:Stream agent logs'
    )

    _arguments -C \
        '1: :->subcmd' \
        '*::arg:->args'

    case $state in
        subcmd)
            _describe 'agent commands' agent_commands
            ;;

        args)
            case $line[1] in
                list)
                    _arguments \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;

                status|logs)
                    _arguments \
                        '1:id:_secrethub_complete_agents' \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;
            esac
            ;;
    esac
}

_secrethub_config_commands() {
    local curcontext="$curcontext" state line
    typeset -A opt_args

    local -a config_commands
    config_commands=(
        'list:List all configuration'
        'get:Get configuration value'
        'set:Set configuration value'
    )

    local -a config_keys
    config_keys=(
        'server_url:SecretHub server URL'
        'default_format:Default output format'
        'log_level:Logging level'
    )

    _arguments -C \
        '1: :->subcmd' \
        '*::arg:->args'

    case $state in
        subcmd)
            _describe 'config commands' config_commands
            ;;

        args)
            case $line[1] in
                list)
                    _arguments \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;

                get)
                    _arguments \
                        '1:key:_secrethub_complete_config_keys' \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;

                set)
                    _arguments \
                        '1:key:_secrethub_complete_config_keys' \
                        '2:value:_secrethub_complete_config_value' \
                        '(-f --format)'{-f,--format}'[Output format]:format:(json table yaml)' \
                        '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
                        '--verbose[Detailed output]'
                    ;;
            esac
            ;;
    esac
}

# Completion helpers for dynamic data
_secrethub_complete_secrets() {
    local config_file="${HOME}/.secrethub/config.toml"
    if [[ -f "${config_file}" ]]; then
        local secrets=(${(f)"$(secrethub secret list --format json 2>/dev/null | \
            grep -o '"path":"[^"]*"' | cut -d'"' -f4)"})
        _describe 'secret paths' secrets
    fi
}

_secrethub_complete_policies() {
    local config_file="${HOME}/.secrethub/config.toml"
    if [[ -f "${config_file}" ]]; then
        local policies=(${(f)"$(secrethub policy list --format json 2>/dev/null | \
            grep -o '"name":"[^"]*"' | cut -d'"' -f4)"})
        _describe 'policy names' policies
    fi
}

_secrethub_complete_agents() {
    local config_file="${HOME}/.secrethub/config.toml"
    if [[ -f "${config_file}" ]]; then
        local agents=(${(f)"$(secrethub agent list --format json 2>/dev/null | \
            grep -o '"id":"[^"]*"' | cut -d'"' -f4)"})
        _describe 'agent IDs' agents
    fi
}

_secrethub_complete_policy_templates() {
    local templates=(
        'admin:Full administrative access'
        'read_only:Read-only access to secrets'
        'business_hours:Access restricted to business hours'
        'time_limited:Time-limited access policy'
        'emergency_access:Emergency break-glass access'
    )
    _describe 'policy templates' templates
}

_secrethub_complete_config_keys() {
    local keys=(
        'server_url:SecretHub server URL'
        'default_format:Default output format (json|table|yaml)'
        'log_level:Logging level (debug|info|warn|error)'
    )
    _describe 'configuration keys' keys
}

_secrethub_complete_config_value() {
    local key="${line[2]}"
    case "$key" in
        default_format)
            _values 'format' json table yaml
            ;;
        log_level)
            _values 'log level' debug info warn error
            ;;
        server_url)
            _urls
            ;;
    esac
}

_secrethub "$@"
