# Git Worktree Manager — Fish integration
# Add to config.fish: gwtm init-shell | source

function gwtm
    switch $argv[1]
        case cd
            if test (count $argv) -lt 2
                echo "Usage: gwtm cd <name|main>" >&2
                return 1
            end
            set -l dir (command gwtm cd $argv[2] 2>/dev/null)
            if test -n "$dir" -a -d "$dir"
                builtin cd "$dir"
            else
                command gwtm cd $argv[2]
                return 1
            end
        case '*'
            command gwtm $argv
    end
end

# Tab completion
complete -c gwtm -f
complete -c gwtm -n "not __fish_seen_subcommand_from new ls rm cd path clone sync status clean init-shell" -a "new" -d "Create a new worktree"
complete -c gwtm -n "not __fish_seen_subcommand_from new ls rm cd path clone sync status clean init-shell" -a "ls" -d "List worktrees"
complete -c gwtm -n "not __fish_seen_subcommand_from new ls rm cd path clone sync status clean init-shell" -a "rm" -d "Remove a worktree"
complete -c gwtm -n "not __fish_seen_subcommand_from new ls rm cd path clone sync status clean init-shell" -a "cd" -d "Change to worktree directory"
complete -c gwtm -n "not __fish_seen_subcommand_from new ls rm cd path clone sync status clean init-shell" -a "path" -d "Print worktree path"
complete -c gwtm -n "not __fish_seen_subcommand_from new ls rm cd path clone sync status clean init-shell" -a "clone" -d "Clone a repo with gwtm setup"
complete -c gwtm -n "not __fish_seen_subcommand_from new ls rm cd path clone sync status clean init-shell" -a "sync" -d "Sync worktree with base branch"
complete -c gwtm -n "not __fish_seen_subcommand_from new ls rm cd path clone sync status clean init-shell" -a "status" -d "Show worktree status"
complete -c gwtm -n "not __fish_seen_subcommand_from new ls rm cd path clone sync status clean init-shell" -a "clean" -d "Remove merged worktrees"
complete -c gwtm -n "not __fish_seen_subcommand_from new ls rm cd path clone sync status clean init-shell" -a "init-shell" -d "Print shell integration code"

# Dynamic worktree name completion for cd, rm, path, sync
function __gwtm_worktree_names
    command gwtm ls --names-only 2>/dev/null
end

complete -c gwtm -n "__fish_seen_subcommand_from cd" -a "(__gwtm_worktree_names)" -d "Worktree"
complete -c gwtm -n "__fish_seen_subcommand_from cd" -a "main" -d "Main worktree"
complete -c gwtm -n "__fish_seen_subcommand_from rm" -a "(__gwtm_worktree_names)" -d "Worktree"
complete -c gwtm -n "__fish_seen_subcommand_from path" -a "(__gwtm_worktree_names)" -d "Worktree"
complete -c gwtm -n "__fish_seen_subcommand_from sync" -a "(__gwtm_worktree_names)" -d "Worktree"
