#!/bin/bash
# Bash completion for backup scripts in /home/acedanger/shell
# Source this file to enable tab completion for backup script flags

# Completion function for backup-immich.sh
_backup_immich_completion() {
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Available options for backup-immich.sh
    opts="--help -h --dry-run --no-upload --verbose"

    # Handle specific option arguments
    case "${prev}" in
        --webhook)
            # Could provide common webhook URLs here
            COMPREPLY=( $(compgen -W "https://notify.peterwood.rocks/lab" -- ${cur}) )
            return 0
            ;;
        *)
            ;;
    esac

    # Standard flag completion
    if [[ ${cur} == -* ]]; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}

# Completion function for backup-plex.sh
_backup_plex_completion() {
    local cur prev opts
    local common_emails="peter@peterwood.dev acedanger49@gmail.com alerts@peterwood.dev"
    
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Available options for backup-plex.sh
    opts="--help -h --auto-repair --check-integrity --non-interactive --no-parallel --no-performance --webhook --email"

    # Handle specific option arguments
    case "${prev}" in
        --webhook)
            COMPREPLY=( $(compgen -W "https://notify.peterwood.rocks/lab" -- ${cur}) )
            return 0
            ;;
        --email)
            # Provide common email addresses for completion
            COMPREPLY=( $(compgen -W "${common_emails}" -- ${cur}) )
            return 0
            ;;
        *)
            ;;
    esac

    # Standard flag completion
    if [[ ${cur} == -* ]]; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}

# Completion function for backup-media.sh
_backup_media_completion() {
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Available options for backup-media.sh
    opts="--help -h --dry-run --no-verify --sequential --interactive --webhook"

    # Handle specific option arguments
    case "${prev}" in
        --webhook)
            COMPREPLY=( $(compgen -W "https://notify.peterwood.rocks/lab" -- ${cur}) )
            return 0
            ;;
        *)
            ;;
    esac

    # Standard flag completion
    if [[ ${cur} == -* ]]; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}

# Completion function for generic backup scripts (fallback)
_backup_generic_completion() {
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Common backup script options
    opts="--help -h --dry-run --verbose --no-upload --webhook"

    # Handle specific option arguments
    case "${prev}" in
        --webhook)
            COMPREPLY=( $(compgen -W "https://notify.peterwood.rocks/lab" -- ${cur}) )
            return 0
            ;;
        *)
            ;;
    esac

    # Standard flag completion
    if [[ ${cur} == -* ]]; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}

# Register completion functions for specific scripts
complete -F _backup_immich_completion backup-immich.sh
complete -F _backup_immich_completion ./backup-immich.sh
complete -F _backup_immich_completion /home/acedanger/shell/immich/backup-immich.sh

complete -F _backup_plex_completion backup-plex.sh
complete -F _backup_plex_completion ./backup-plex.sh
complete -F _backup_plex_completion /home/acedanger/shell/plex/backup-plex.sh

complete -F _backup_media_completion backup-media.sh
complete -F _backup_media_completion ./backup-media.sh
complete -F _backup_media_completion /home/acedanger/shell/backup-media.sh

# Register completion for other backup scripts with generic completion
complete -F _backup_generic_completion backup-docker.sh
complete -F _backup_generic_completion ./backup-docker.sh
complete -F _backup_generic_completion /home/acedanger/shell/backup-docker.sh

# You can add more specific completions here for other scripts
# Example:
# complete -F _backup_generic_completion your-other-backup-script.sh

# Enable completion for script files when run with explicit paths
_backup_script_path_completion() {
    local cur="${COMP_WORDS[COMP_CWORD]}"
    
    # If the current word looks like a backup script path, provide flag completion
    if [[ "${COMP_WORDS[0]}" == *backup*.sh ]]; then
        # Use generic completion for path-based calls
        _backup_generic_completion
    fi
}

# Register for any script ending in backup*.sh
complete -o default -F _backup_script_path_completion -X '!*backup*.sh'
