#!/usr/bin/env bash

# The function that will perform the autocomplete
_cijoe_completion() {
    local cur prev yaml_file toml_file values options

    # Get the current word (the one being completed) and the previous word
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Define the possible options for cijoe
    options="--help --config -c --workflow -w --output -o --log-level -l --no-report -n --skip-report -s --tag -t --archive -a --produce-report -p --monitor -m --integrity-check -i --resources -r --example -e --version -v"

    # Provide options completion if the current word starts with --
    if [[ "$cur" == --* ]]; then
        COMPREPLY=( $(compgen -W "$options" -- "$cur") )
        return
    fi

    # Check if the previous word is --config or -c, which specifies the TOML file
    if [[ "$prev" == "--config" || "$prev" == "-c" ]]; then
        # Complete with TOML files or directories (allow both files and paths)
        COMPREPLY=( $(compgen -o plusdirs -f -- "$cur") )
        for i in "${!COMPREPLY[@]}"; do
            # Append "/" to directories only if needed and ensure it doesn't append for the full path
            if [[ -d "${COMPREPLY[$i]}" ]]; then
                if [[ "${cur}" != */ ]]; then
                    COMPREPLY[$i]="${COMPREPLY[$i]}/"  # Append "/" to directories only if it doesn't already have one
                fi
            elif [[ ! "${COMPREPLY[$i]}" == *.toml ]]; then
                unset "COMPREPLY[$i]"  # Remove non-TOML files
            fi
        done
        return
    fi

    # Check if the previous word is --workflow or -w, which specifies the YAML file
    if [[ "$prev" == "--workflow" || "$prev" == "-w" ]]; then
        # Complete with YAML files or directories (allow both files and paths)
        COMPREPLY=( $(compgen -o plusdirs -f -- "$cur") )
        for i in "${!COMPREPLY[@]}"; do
            # Append "/" to directories only if needed and ensure it doesn't append for the full path
            if [[ -d "${COMPREPLY[$i]}" ]]; then
                if [[ "${cur}" != */ ]]; then
                    COMPREPLY[$i]="${COMPREPLY[$i]}/"  # Append "/" to directories only if it doesn't already have one
                fi
            elif [[ ! "${COMPREPLY[$i]}" == *.yaml ]]; then
                unset "COMPREPLY[$i]"  # Remove non-YAML files
            fi
        done
        return
    fi

    # Search for the YAML file in the command-line arguments
    yaml_file=""
    for ((i=1; i<COMP_CWORD; i++)); do
        if [[ "${COMP_WORDS[i]}" == "--workflow" || "${COMP_WORDS[i]}" == "-w" ]]; then
            yaml_file="${COMP_WORDS[i+1]}"
            break
        fi
    done

    # If no YAML file is provided, use 'cijoe-workflow.yaml' as the default
    if [[ -z "$yaml_file" ]]; then
        yaml_file="cijoe-workflow.yaml"
    fi

    # If the YAML file doesn't exist, return without suggestions
    if [[ ! -f "$yaml_file" ]]; then
        return
    fi

    # Extract 'name' values from the YAML file under 'steps'
    values=$(grep -oP '(?<=name: ).*' "$yaml_file")

    # Provide the extracted values as autocomplete suggestions
    COMPREPLY=( $(compgen -W "${values}" -- "$cur") )
}

# Register the completion function for your cijoe command
complete -F _cijoe_completion cijoe
