#compdef sessionx
# zsh completion for sessionx

_sessionx() {
    local -a commands
    commands=(
        'init:scaffold .sessionx.yaml in cwd'
        'edit:open .sessionx.yaml in $EDITOR'
        'add:create + attach a session'
        'ls:list sessions managed by sessionx'
        'open:attach to any sessionx-managed session globally'
        'rm:tear down a session (and worktree)'
        'completions:print shell completion script'
        'themes:list built-in status-bar themes'
        'theme:manage project theme (set/preview/list)'
        'hooks:manage stack-specific hook recipes (list/info/install/update/repo)'
        'config:manage global config (~/.config/sessionx/config.yaml)'
    )

    local context curcontext="$curcontext" state line ret=1
    typeset -A opt_args

    _arguments -C \
        '(-v --verbose)'{-v,--verbose}'[print tmux/git commands]' \
        '(-h --help)'{-h,--help}'[show help]' \
        '(-V --version)'{-V,--version}'[show version]' \
        '1: :->cmd' \
        '*:: :->args' && ret=0

    case $state in
        cmd)
            _describe -t commands 'sessionx command' commands && ret=0
            ;;
        args)
            case $words[1] in
                add)
                    _arguments \
                        '--base[base ref to branch from]:ref:' \
                        '--no-attach[do not attach to the session]' \
                        '1:name:' && ret=0
                    ;;
                rm)
                    _arguments \
                        '--force[force removal]' \
                        '1:session:_sessionx_handles' && ret=0
                    ;;
                open)
                    _arguments \
                        '1:session:_sessionx_managed' && ret=0
                    ;;
                completions)
                    _values 'shell' bash zsh fish && ret=0
                    ;;
                theme)
                    _arguments -C \
                        '--no-apply[write yaml only]' \
                        '--session[target session]:session:_sessionx_managed' \
                        '1: :->themearg1' \
                        '*:: :->themeargs' && ret=0
                    case $state in
                        themearg1)
                            local -a theme_actions
                            theme_actions=(
                                'set:set + apply theme'
                                'preview:try a theme without saving'
                            )
                            _alternative \
                                'actions:action:(($theme_actions))' \
                                'themes:theme:_sessionx_theme_names' && ret=0
                            ;;
                        themeargs)
                            case $words[1] in
                                set|preview)
                                    _arguments \
                                        '--no-apply[write yaml only]' \
                                        '--session[target session]:session:_sessionx_managed' \
                                        '1:theme:_sessionx_theme_names' && ret=0
                                    ;;
                            esac
                            ;;
                    esac
                    ;;
                ls)
                    _arguments \
                        '--names-only[print only handles]' \
                        '--all[list all managed sessions globally]' && ret=0
                    ;;
                hooks)
                    _arguments -C \
                        '1: :->hookarg1' \
                        '*:: :->hookargs' && ret=0
                    case $state in
                        hookarg1)
                            local -a hook_actions
                            hook_actions=(
                                'list:show available recipes'
                                'info:describe a recipe'
                                'install:install a recipe to ~/.sessionx/scripts/<id>/'
                                'update:refresh the cache'
                                'repo:print repo URL + ref + cache path'
                            )
                            _describe -t actions 'hooks action' hook_actions && ret=0
                            ;;
                        hookargs)
                            case $words[1] in
                                info|install)
                                    _arguments \
                                        '1:recipe:_sessionx_recipes' && ret=0
                                    ;;
                            esac
                            ;;
                    esac
                    ;;
                config)
                    _arguments -C \
                        '1: :->cfgarg1' \
                        '*:: :->cfgargs' && ret=0
                    case $state in
                        cfgarg1)
                            local -a cfg_actions
                            cfg_actions=(
                                'path:print config file path'
                                'get:dump config or read a key'
                                'set:write a key=value'
                            )
                            _describe -t actions 'config action' cfg_actions && ret=0
                            ;;
                        cfgargs)
                            case $words[1] in
                                get)
                                    _values 'key' agent && ret=0
                                    ;;
                                set)
                                    _arguments \
                                        '1:key:(agent)' \
                                        '2:value:' && ret=0
                                    ;;
                            esac
                            ;;
                    esac
                    ;;
            esac
            ;;
    esac
    return ret
}

_sessionx_handles() {
    local -a h
    h=(${(f)"$(sessionx ls --names-only 2>/dev/null)"})
    if (( ${#h} )); then
        _describe -t handles 'session' h
    fi
}

_sessionx_managed() {
    local -a h
    h=(${(f)"$(sessionx open --names-only 2>/dev/null)"})
    if (( ${#h} )); then
        _describe -t managed 'managed session' h
    fi
}

_sessionx_recipes() {
    local -a h
    # Lazy: parse `sessionx hooks list` output (lines look like `  laravel-herd  [php]  ...`).
    h=(${(f)"$(sessionx hooks list 2>/dev/null | awk '/^  [a-z]/{print $1}')"})
    if (( ${#h} )); then
        _describe -t recipes 'recipe' h
    fi
}

_sessionx_theme_names() {
    local -a h
    h=(${(f)"$(sessionx themes 2>/dev/null)"})
    if (( ${#h} )); then
        _describe -t themes 'theme' h
    fi
}

compdef _sessionx sessionx
