# Load shared utility functions
source src/cmd.sh

# Function to configure the default shell and install helper "binaries" instead of aliases
function shell_config() {
    # 1. Detect AUR helper and prepare command mappings
    local aur_helper="${AUR}"  # expected to be 'yay' or 'paru'

    # Declare an associative array mapping command names to their implementations
    declare -A cmd_map=(
        [fix-key]="sudo rm /var/lib/pacman/sync/* && \
sudo rm -rf /etc/pacman.d/gnupg/* && \
sudo pacman-key --init && \
sudo pacman-key --populate && \
sudo pacman -Sy --noconfirm archlinux-keyring && \
sudo pacman --noconfirm -Su"
        [update-arch]="${aur_helper} -Syu --noconfirm"
        [update-grub]="sudo grub-mkconfig -o /boot/grub/grub.cfg"
        [install-all-pkg]="sudo pacman -S \$(pacman -Qnq) --overwrite '*'"
    )

    # Add the clean-arch command based on the chosen AUR helper
    if [[ "${aur_helper}" == "yay" ]]; then
        cmd_map[clean-arch]="yay -Sc --noconfirm && yay -Yc --noconfirm"
    elif [[ "${aur_helper}" == "paru" ]]; then
        cmd_map[clean-arch]="paru -Sc --noconfirm && paru -c --noconfirm"
    fi

    # 2. Create or recreate executable scripts in /usr/bin for each command
    for name in "${!cmd_map[@]}"; do
        local cmd="${cmd_map[$name]}"

        # If the binary already exists, remove it first
        if [[ -f /usr/bin/${name} ]]; then
            exec_log "sudo rm /usr/bin/${name}" "$(eval_gettext "Removing existing /usr/bin/${name}")"
        fi

        # Use tee with an EOF block to install the script
        exec_log "sudo tee /usr/bin/${name} > /dev/null << 'EOF'
#!/bin/bash
# Auto-generated by shell_config(): ${name}
${cmd}
EOF" "$(eval_gettext "Creating /usr/bin/${name}")"

        # Make the script executable
        exec_log "sudo chmod +x /usr/bin/${name}" "$(eval_gettext "Making /usr/bin/${name} executable")"
    done

    # 3. Prompt the user to select their default shell (bash, zsh, or fish)
    local choice=""
    while [[ "${choice}" != "bash" && "${choice}" != "zsh" && "${choice}" != "fish" ]]; do
        read -rp "$(eval_gettext "What is your default shell? (bash/zsh/fish): ")" choice
        choice="${choice,,}"  # convert to lowercase
    done

    eval_gettext "${GREEN}You chose ${choice}${RESET}"; echo

    case "${choice}" in
        bash)
            # Ensure ~/.bashrc exists
            touch "${HOME}/.bashrc"
            ;;
        zsh)
            # Install zsh and completions
            install_one "zsh"
            install_one "zsh-completions"

            # Change default shell to zsh if not already
            local current_shell
            current_shell=$(getent passwd "$USER" | cut -d: -f7)
            while [[ "${current_shell}" != "/usr/bin/zsh" ]]; do
                eval_gettext "Switching default shell to zsh..."; echo
                chsh -s "/usr/bin/zsh"
                current_shell=$(getent passwd "$USER" | cut -d: -f7)
            done

            # Optionally install oh-my-zsh
            if ask_question "$(eval_gettext "Do you want to install oh-my-zsh?")"; then
                eval_gettext "Installing oh-my-zsh..."; echo
                git clone https://github.com/ohmyzsh/ohmyzsh.git "${HOME}/.oh-my-zsh"
                cp "${HOME}/.oh-my-zsh/templates/zshrc.zsh-template" "${HOME}/.zshrc"
            fi
            ;;
        fish)
            # Install fish shell
            install_one "fish"

            # Change default shell to fish if not already
            local current_shell
            current_shell=$(getent passwd "$USER" | cut -d: -f7)
            while [[ "${current_shell}" != "/usr/bin/fish" ]]; do
                eval_gettext "Switching default shell to fish..."; echo
                chsh -s "/usr/bin/fish"
                current_shell=$(getent passwd "$USER" | cut -d: -f7)
            done

            # Update fish completions and clear default greeting
            fish -c 'fish_update_completions'
            fish -c 'set -U fish_greeting'

            # Ensure fish config directory and file exist
            mkdir -p "${HOME}/.config/fish"
            touch "${HOME}/.config/fish/config.fish"
            ;;
    esac

    eval_gettext "Shell configuration complete!"
}
