#!/bin/bash

appUninstallMenu() {
    while true; do
        isHeader "Installed Apps"
        echo ""
        isNotice "Scanning for installed applications..."
        echo ""
        
        local installed_apps=()
        
        # Get all installed apps by checking for docker-compose.yml files
        for app_dir in "$containers_dir"/*/; do
            if [ -d "$app_dir" ]; then
                local compose_file="$app_dir/docker-compose.yml"
                if [ -f "$compose_file" ]; then
                    local app_name=$(basename "$app_dir")
                    installed_apps+=("$app_name")
                fi
            fi
        done
        
        if [ ${#installed_apps[@]} -eq 0 ]; then
            isNotice "No installed applications found."
            continue
        fi
        
        # Show numbered list of installed apps
        echo ""
        for i in "${!installed_apps[@]}"; do
            isOption "$((i + 1)). ${installed_apps[$i]}"
        done
        echo ""
        isOption "x. Exit"
        echo ""
        isQuestion "Please select an app to uninstall (1-${#installed_apps[@]} or 'x' to exit): "
        read -p "" choice
        
        if [[ "$choice" =~ ^[1-9][0-9]*$ ]] && [ "$choice" -le "${#installed_apps[@]}" ]; then
            local selected_app="${installed_apps[$((choice - 1))]}"
            dockerUninstallApp $selected_app
            endStart
        elif [[ "$choice" == "x" ]]; then
            isNotice "Exiting..."
        else
            isNotice "Invalid selection. Please choose a valid number or 'x' to exit."
        fi
    done
}
