#compdef lisa
# Zsh completion for `lisa`.
#
# Install:
#   mkdir -p ~/.zsh/completions
#   cp _lisa ~/.zsh/completions/_lisa
#   # Add to ~/.zshrc (if not already):
#   #   fpath=(~/.zsh/completions $fpath)
#   #   autoload -U compinit && compinit
#
# Reload your shell.

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

    _arguments -C \
        '(-h --help)'{-h,--help}'[show help]' \
        '(-v --version)'{-v,--version}'[print version and exit]' \
        '--model[LLM model name]:model:_lisa_models' \
        '--provider[force provider]:provider:(anthropic openai gemini)' \
        '--think[enable adaptive thinking each turn]' \
        '--no-reflect[skip end-of-session reflection]' \
        '--compact[enable Anthropic context compaction]' \
        '--approval[approval mode]:mode:(auto ask ask-mutating)' \
        '--no-mcp[skip loading MCP servers]' \
        '--no-plugins[skip loading plugins]' \
        '--voice[enable speak/transcribe tools]' \
        '--idle[idle minutes before autonomous mode]:minutes:' \
        '--no-idle[disable idle mode]' \
        '--web[start web UI (with serve)]' \
        '--imessage[start iMessage channel (with serve)]' \
        '--channels[start IM channels]:list:_lisa_channels' \
        '--port[port for serve --web]:port:' \
        '--verbose[startup banners + full tool results]' \
        '--no-color[plain output even on a terminal]' \
        '--host[bind address for serve --web]:addr:' \
        '1: :_lisa_first' \
        '*::arg:->args' \
        && return 0

    case "$state" in
        args)
            case "$line[1]" in
                skills)   _lisa_skills ;;
                heartbeat) _lisa_heartbeat ;;
                autostart) _lisa_autostart ;;
                model)    _lisa_model ;;
                doctor)   _lisa_doctor ;;
                upgrade)  _lisa_upgrade ;;
                consent)  _lisa_consent ;;
                sense)    _lisa_sense ;;
            esac
            ;;
    esac
}

_lisa_first() {
    local -a subcommands
    subcommands=(
        'resume:resume a previous session by id'
        'sessions:list recent sessions'
        'serve:start web UI / IM channels'
        'heartbeat:run / install / uninstall scheduled tasks'
        'autostart:run the backend from login (install/uninstall/status)'
        'upgrade:upgrade this install and restart the daemon'
        'search:TF-IDF search across past sessions'
        'birth:run the birth ritual'
        'soul:print current soul summary'
        'channels:list available channel adapters'
        'skills:manage executable skills (list/approve/disable/enable/audit)'
        'wishlist:print Lisa'\''s feedback about her toolset'
        'status:one-shot snapshot'
        'doctor:health check'
        'monitor:TUI live dashboard'
        'autonomy:summarize recent autonomous runs'
        'model:local model lifecycle (list/install/use/health)'
        'consent:consent for sensitive ambient signals'
        'sense:recent ambient sense events'
        'agents:snapshot of agent sessions across observers'
        'pair:show a QR to pair a phone (Lisa Pocket)'
        'mail:mail account setup and inbox commands'
        'kb:knowledge base (add/list/search/brief)'
        'login:sign in to Lisa Cloud'
        'logout:sign out of Lisa Cloud'
        'billing:allowance, credits and recent usage'
    )
    _describe 'subcommand' subcommands
}

_lisa_skills() {
    if (( CURRENT == 2 )); then
        local -a actions
        actions=(
            'list:list executable skills'
            'approve:review and approve a skill'
            'disable:block a skill from loading'
            'enable:remove a disable flag'
            'audit:show the audit trail'
        )
        _describe 'skills action' actions
    elif (( CURRENT == 3 )); then
        # 3rd arg = slug; suggest from ~/.lisa/skills/
        local -a slugs
        if [[ -d "$HOME/.lisa/skills" ]]; then
            slugs=("$HOME/.lisa/skills"/*(/N:t))
            _describe 'skill slug' slugs
        fi
    fi
}

_lisa_heartbeat() {
    if (( CURRENT == 2 )); then
        local -a actions
        actions=(
            'run:run heartbeat tasks once'
            'install:install macOS launchd plist'
            'uninstall:remove launchd plist'
        )
        _describe 'heartbeat action' actions
    fi
}

_lisa_autostart() {
    if (( CURRENT == 2 )); then
        local -a actions
        actions=(
            'install:keep serve --web running at login + on crash'
            'uninstall:stop starting Lisa at login'
            'status:show whether autostart is installed / loaded'
        )
        _describe 'autostart action' actions
    fi
}

_lisa_doctor() {
    _arguments '--probe[probe a running backend'\''s /health]:url:_urls'
}

_lisa_upgrade() {
    local -a flags
    flags=(
        '--check:only compare installed vs published version'
        '--dry-run:print the commands without running them'
    )
    _describe 'upgrade flag' flags
}

_lisa_model() {
    if (( CURRENT == 2 )); then
        local -a actions
        actions=(
            'list:list installed local models + backend health'
            'install:pull a local model'
            'use:switch to local://<model>'
            'health:check the local backend'
        )
        _describe 'model action' actions
    fi
}

_lisa_consent() {
    if (( CURRENT == 2 )); then
        local -a actions
        actions=(
            'list:show signals + status'
            'grant:grant a signal'
            'revoke:revoke a signal'
            'revoke-all:stop all sensing'
        )
        _describe 'consent action' actions
    elif (( CURRENT == 3 )) && [[ "$line[2]" == (grant|revoke) ]]; then
        _describe 'signal' '(screen voice clipboard selection)'
    fi
}

_lisa_sense() {
    if (( CURRENT == 2 )); then
        _describe 'sense action' '(list:show recent ambient events + granted signals)'
    fi
}

_lisa_models() {
    local -a models
    models=(
        # Anthropic
        'claude-sonnet-4-5-20250929:Claude Sonnet 4.5'
        'claude-opus-4:Claude Opus 4'
        'claude-haiku-4:Claude Haiku 4 (cheap)'
        # OpenAI
        'gpt-5:GPT-5'
        'gpt-4o:GPT-4o'
        'o3:OpenAI o3'
        'o4-mini:OpenAI o4-mini (cheap)'
        # Google
        'gemini-2.5-pro:Gemini 2.5 Pro'
        'gemini-2.5-flash:Gemini 2.5 Flash (cheap)'
        # DeepSeek
        'deepseek-chat:DeepSeek V3 (cheap)'
        'deepseek-reasoner:DeepSeek reasoning'
        # Volcengine Doubao
        'doubao-1.5-pro-32k:Doubao 1.5 Pro'
        # Aliyun Qwen
        'qwen3-72b-instruct:Qwen 3 72B'
        # Moonshot
        'moonshot-v1-32k:Moonshot v1 (32k)'
        'kimi-128k-vision:Kimi 128k vision'
        # xAI
        'grok-2-latest:Grok 2'
        # Zhipu
        'glm-4.5:GLM 4.5'
    )
    _describe 'model' models
}

_lisa_channels() {
    local -a channels
    channels=(telegram discord slack feishu webhook imessage all)
    _describe -t channels 'channel' channels
}

_lisa "$@"
