sp(){
	case "$1" in
        -l)
            if [[ -n "$2" ]]; then
				local link_name="${3:-$(basename "$2")}"
                ln -s "$(realpath "$2")" "$HOME/.projects/$link_name"
            else
                echo "Usage: sp -l <project to link> [project Name]"
            fi
            ;;
        -u|-r)
            if [[ -n "$2" ]]; then
                local link_path="$HOME/.projects/$(basename "$2")"
				
                if [[ -L "$link_path" ]]; then
                    rm "$link_path"
                else
                    echo "No such project: $2"
                fi
            else
                echo "Usage: sp -u|-r <project to remove>"
            fi
            ;;
        *)
            if [[ -n "$1" ]]; then
                cd -P "$HOME/.projects/$1" 
            else
                echo "Usage: sp [-l|-u|-r] <project Name>"
            fi
            ;;
    esac
}

_sp_complete() {
    local cur="${COMP_WORDS[COMP_CWORD]}"  # Current word being completed
    local prev="${COMP_WORDS[COMP_CWORD-1]}"  # Previous word
    local target_dir="$HOME/.projects"  # Your fixed project directory
    case "$prev" in
        -l)
            COMPREPLY=($(compgen -f -- "$cur"))
            ;;
        *)
			suggestions=$(compgen -o plusdirs -f -- "$target_dir/$cur")
			COMPREPLY=()
			# Loop over the suggestions, strip the base_dir prefix for display.
			for suggestion in $suggestions; do
				# Remove the folder prefix if present
				suggestion=${suggestion#$target_dir/}
				COMPREPLY+=("$suggestion/")
			done
            ;;
    esac
}

# Attach the completion function to `sp`
complete -o nospace -F _sp_complete sp


if ! [ -d ~/.projects ] 
then
	mkdir ~/.projects
fi
