#!/bin/bash
# Bash completion for pieces
# Generated automatically - using Pieces CLI
# Version: 1.16.1

# Provide _init_completion if not available (e.g., on macOS without bash-completion)
if ! declare -F _init_completion >/dev/null 2>&1; then
    _init_completion() {
        local exclude flag outx errx inx OPTIND=1

        while getopts "n:e:o:i:s" flag "$@"; do
            case $flag in
                n) exclude+=$OPTARG ;;
                e) errx=$OPTARG ;;
                o) outx=$OPTARG ;;
                i) inx=$OPTARG ;;
                s) split=false ;;
            esac
        done

        # Set cur, prev, words, and cword
        cur="${COMP_WORDS[COMP_CWORD]}"
        prev="${COMP_WORDS[COMP_CWORD-1]}"
        words=("${COMP_WORDS[@]}")
        cword=$COMP_CWORD

        # Handle redirection operators
        case "$prev" in
            ">"|"<"|">>"|"&>"|"2>"|"2>&1")
                return 1
                ;;
        esac

        # Handle = in variable assignments
        if [[ $cur == *=* ]]; then
            prev=${cur%%=*}
            cur=${cur#*=}
        fi

        return 0
    }
fi

# Provide _filedir if not available
if ! declare -F _filedir >/dev/null 2>&1; then
    _filedir() {
        local IFS=$'\n'
        
        # Simple file/directory completion
        if [[ -z "$1" ]]; then
            # Complete with files and directories
            COMPREPLY=( $(compgen -f -- "$cur") )
        else
            # Complete with specific extension
            COMPREPLY=( $(compgen -f -X '!*'"$1" -- "$cur") )
        fi
        
        # Mark directories with trailing slash
        for ((i=0; i < ${#COMPREPLY[@]}; i++)); do
            if [[ -d "${COMPREPLY[i]}" ]]; then
                COMPREPLY[i]+="/"
            fi
        done
    }
fi

_pieces_completion() {
    local cur prev words cword
    _init_completion || return

    local cmd i
    
    # Find the command (first non-option argument after pieces)
    cmd=""
    for ((i=1; i < cword; i++)); do
        if [[ "${words[i]}" != -* ]]; then
            cmd="${words[i]}"
            break
        fi
    done

    # Define global options
    local global_options="--help -h --version -v --ignore-onboarding"

    # If we haven't found a command yet, complete with commands
    if [[ -z "$cmd" ]] || [[ "$cword" -eq 1 ]]; then
        # Show commands with descriptions when completing
        if [[ "$cur" == -* ]]; then
            # If current word starts with -, show options
            COMPREPLY=( $(compgen -W "$global_options" -- "$cur") )
        else
            # Show available commands
            local commands="ask chat chats commit completion config contribute conversation conversations create delete drive edit execute feedback install list login logout mcp modify onboarding open run save search share version"
            COMPREPLY=( $(compgen -W "$commands" -- "$cur") )
        fi
        return 0
    fi

    # Normalize command (convert alias to main command)
    case "$cmd" in
        drive)
            cmd="list"
            ;;
        save)
            cmd="modify"
            ;;
        conversations)
            cmd="chats"
            ;;
        conversation)
            cmd="chat"
            ;;
    esac

    # Handle command-specific completions
    case "$cmd" in
        config)
            # Configure various Pieces CLI settings including default editor and other preferences
            local cmd_options="-h --help --editor -e"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        list)
            # List and browse various Pieces resources including code materials, connected applications, and available AI models. Use the editor flag to open snippets directly in your default editor.
            local cmd_options="-h --help --editor -e"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        login)
            # Authenticate with PiecesOS to enable cloud features, and access your personal domain, long term memory
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        logout)
            # Sign out from your PiecesOS account, disabling the use of any of the Pieces features
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        search)
            # Search through your materials using various search modes including fuzzy search, neural code search, and full text search
            local cmd_options="-h --help --mode"
            
            case "$prev" in
                --mode)
                    # Complete with predefined choices
                    COMPREPLY=( $(compgen -W "fuzzy ncs fts" -- "$cur") )
                    return 0
                    ;;
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        modify)
            # Save or update changes to the currently selected material. Use this after making modifications to persist your changes
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        delete)
            # Permanently delete the currently selected material from your Pieces database. This action cannot be undone
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        create)
            # Create a new code snippet or material in your Pieces database. You can create a snippet from clipboard content or enter it manually.
            local cmd_options="-h --help -c --content"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        share)
            # Generate a shareable link for the currently selected material, allowing others to view and access your code snippet
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        edit)
            # Edit properties of an existing material including its name, language classification, and other metadata
            local cmd_options="-h --help --name -n --classification -c"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        run)
            # Run the Pieces CLI in interactive loop mode, allowing you to execute multiple commands sequentially without restarting the CLI
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        execute)
            # Execute shell or bash code snippets directly from your saved materials, making it easy to run saved scripts and commands
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        feedback)
            # Submit feedback, bug reports, or feature requests to help improve the Pieces CLI. Your feedback is invaluable for making the tool better
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        contribute)
            # Learn how to contribute to the Pieces CLI project, including guidelines for submitting pull requests, reporting issues, and improving documentation
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        install)
            # Install or update PiecesOS, the local runtime that powers all Pieces applications. This command will download and set up PiecesOS for your platform
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        onboarding)
            # Start the interactive onboarding process to set up Pieces CLI, configure settings, and learn about key features through a guided tutorial
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        version)
            # Display version information for both Pieces CLI and PiecesOS, including build numbers and compatibility details
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        ask)
            # Ask questions to the Pieces Copilot AI assistant with context from files, saved materials, or your long-term memory (LTM). Get intelligent code assistance and explanations
            local cmd_options="-h --help --files -f --materials -m --ltm"
            
            case "$prev" in
                --files)
                    # Complete with files and directories
                    _filedir
                    return 0
                    ;;
                -f)
                    # Complete with files and directories
                    _filedir
                    return 0
                    ;;
                --materials)
                    # Complete with numeric values
                    COMPREPLY=( $(compgen -W "1 2 3 4 5 6 7 8 9 10" -- "$cur") )
                    return 0
                    ;;
                -m)
                    # Complete with numeric values
                    COMPREPLY=( $(compgen -W "1 2 3 4 5 6 7 8 9 10" -- "$cur") )
                    return 0
                    ;;
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        chats)
            # Display a list of all your saved conversations with the Pieces Copilot, showing titles and timestamps for easy navigation
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        chat)
            # Manage individual conversations with the Pieces Copilot. You can select, create, rename, or delete conversations
            local cmd_options="-h --help -n --new -r --rename -d --delete"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        commit)
            # Automatically generate meaningful commit messages based on your code changes using AI, with options to stage files, add issue references, and push to remote
            local cmd_options="-h --help -p --push -a --all -i --issues"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        open)
            # Open various Pieces applications and components including PiecesOS, Copilot, Drive, and Settings from the command line
            local cmd_options="-h --help -p --pieces_os -c --copilot -d --drive -s --settings"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        mcp)
            # Manage Model Context Protocol (MCP) integrations for various IDEs and tools including VS Code, Cursor, Claude Desktop, and Goose
            
            # Find subcommand
            local subcmd=""
            for ((i=i+1; i < cword; i++)); do
                if [[ "${words[i]}" != -* ]]; then
                    subcmd="${words[i]}"
                    break
                fi
            done
            
            if [[ -z "$subcmd" ]]; then
                # No subcommand yet, show available subcommands
                local subcommands="docs list repair setup start status"
                COMPREPLY=( $(compgen -W "$subcommands" -- "$cur") )
                return 0
            fi
            
            # Handle subcommand completions
            case "$subcmd" in
                setup)
                    local subcmd_options="-h --help --vscode --goose --cursor --claude --windsurf --zed --shortwave --claude_code --raycast --wrap --globally --specific-workspace --stdio"
                    
                    case "$prev" in
                        *)
                            COMPREPLY=( $(compgen -W "$subcmd_options" -- "$cur") )
                            return 0
                            ;;
                    esac
                    ;;
                list)
                    local subcmd_options="-h --help --already-registered --available-for-setup"
                    
                    case "$prev" in
                        *)
                            COMPREPLY=( $(compgen -W "$subcmd_options" -- "$cur") )
                            return 0
                            ;;
                    esac
                    ;;
                docs)
                    local subcmd_options="-h --help --integration -i --open -o"
                    
                    case "$prev" in
                        --integration)
                            COMPREPLY=( $(compgen -W "vscode goose cursor claude windsurf zed shortwave claude_code all current raycast wrap" -- "$cur") )
                            return 0
                            ;;
                        -i)
                            COMPREPLY=( $(compgen -W "vscode goose cursor claude windsurf zed shortwave claude_code all current raycast wrap" -- "$cur") )
                            return 0
                            ;;
                        *)
                            COMPREPLY=( $(compgen -W "$subcmd_options" -- "$cur") )
                            return 0
                            ;;
                    esac
                    ;;
                start)
                    local subcmd_options="-h --help"
                    
                    case "$prev" in
                        *)
                            COMPREPLY=( $(compgen -W "$subcmd_options" -- "$cur") )
                            return 0
                            ;;
                    esac
                    ;;
                repair)
                    local subcmd_options="-h --help --integration -i"
                    
                    case "$prev" in
                        --integration)
                            COMPREPLY=( $(compgen -W "vscode goose cursor claude windsurf zed shortwave claude_code all" -- "$cur") )
                            return 0
                            ;;
                        -i)
                            COMPREPLY=( $(compgen -W "vscode goose cursor claude windsurf zed shortwave claude_code all" -- "$cur") )
                            return 0
                            ;;
                        *)
                            COMPREPLY=( $(compgen -W "$subcmd_options" -- "$cur") )
                            return 0
                            ;;
                    esac
                    ;;
                status)
                    local subcmd_options="-h --help"
                    
                    case "$prev" in
                        *)
                            COMPREPLY=( $(compgen -W "$subcmd_options" -- "$cur") )
                            return 0
                            ;;
                    esac
                    ;;
                *)
                    # Unknown subcommand
                    return 0
                    ;;
            esac
            ;;
        completion)
            # Display the raw completion script for a specific shell
            local cmd_options="-h --help"
            
            case "$prev" in
                *)
                    # Default option completion
                    COMPREPLY=( $(compgen -W "$cmd_options" -- "$cur") )
                    return 0
                    ;;
            esac
            ;;
        help)
            # Complete with available commands for help
            local commands="ask chat chats commit completion config contribute create delete edit execute feedback install list login logout mcp modify onboarding open run search share version"
            COMPREPLY=( $(compgen -W "$commands" -- "$cur") )
            return 0
            ;;
        *)
            # Unknown command, no completion
            return 0
            ;;
    esac
}

# Helper function to show command descriptions
_pieces_describe_commands() {
    local commands=(
        "ask         Ask a question to the Copilot"
        "chat        Select a chat"
        "chats       Print all chats"
        "commit      Auto-generate a GitHub commit message and commit changes"
        "completion  Display shell completion scripts"
        "config      Configure settings"
        "contribute  How to contribute"
        "create      Create a new material"
        "delete      Delete the current material"
        "edit        Edit an existing material's metadata"
        "execute     Execute shell or bash materials"
        "feedback    Submit feedback"
        "install     Install PiecesOS"
        "list        List materials or apps or models"
        "login       Sign into PiecesOS"
        "logout      Sign out from PiecesOS"
        "mcp         setup the MCP server for an integration"
        "modify      Updates the current material content"
        "onboarding  Start the onboarding process"
        "open        Opens PiecesOS or Applet"
        "run         Runs CLI in a loop"
        "search      Perform a search for materials using the specified query string"
        "share       Share the current material"
        "version     Gets version of PiecesOS"
    )
    
    printf '%s\n' "${commands[@]}"
}

# Register the completion function
complete -F _pieces_completion pieces
