#!/bin/bash
# NIP v0.2.0 "Weihnachtsmann" Uninstaller 🎅
# Official uninstaller for NIP - Nexus Integrated Packager
#
# Usage:
#   curl -L https://git.maiwald.work/Nexus/NexusToolKit/raw/branch/main/nip/uninstall.sh | bash
#   wget -O- https://git.maiwald.work/Nexus/NexusToolKit/raw/branch/main/nip/uninstall.sh | bash

set -e

# Configuration
INSTALL_DIR="/usr/local/bin"
BINARY_NAME="nip"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
NC='\033[0m' # No Color

# Emojis
SANTA="🎅"
CROSS="❌"
CHECK="✅"
INFO="ℹ️"
WARN="⚠️"
TRASH="🗑️"

echo -e "${PURPLE}${SANTA} NIP v0.2.0 'Weihnachtsmann' Uninstaller${NC}"
echo -e "${PURPLE}============================================${NC}"
echo ""

# Function to print colored output
print_status() {
    echo -e "${GREEN}${CHECK} $1${NC}"
}

print_error() {
    echo -e "${RED}${CROSS} $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}${WARN} $1${NC}"
}

print_info() {
    echo -e "${BLUE}${INFO} $1${NC}"
}

# Function to check if running as root
check_root() {
    if [ "$EUID" -ne 0 ]; then
        print_error "This uninstaller requires root privileges"
        echo -e "${YELLOW}${INFO} Please run with sudo:${NC}"
        echo -e "${BLUE}  curl -L https://git.maiwald.work/Nexus/NexusToolKit/raw/branch/main/nip/uninstall.sh | sudo bash${NC}"
        exit 1
    fi
}

# Function to confirm uninstallation
confirm_uninstall() {
    echo -e "${YELLOW}${WARN} This will remove NIP and optionally its data${NC}"
    echo ""
    read -p "Are you sure you want to uninstall NIP? [y/N]: " -r
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo -e "${BLUE}${INFO} Uninstallation cancelled${NC}"
        exit 0
    fi
}

# Function to remove binary
remove_binary() {
    if [ -f "$INSTALL_DIR/$BINARY_NAME" ]; then
        rm -f "$INSTALL_DIR/$BINARY_NAME"
        print_status "Removed NIP binary from $INSTALL_DIR/$BINARY_NAME"
    else
        print_info "NIP binary not found in $INSTALL_DIR"
    fi
}

# Function to remove system integration
remove_system_integration() {
    # Remove PATH configuration
    if [ -f "/etc/profile.d/nip.sh" ]; then
        rm -f "/etc/profile.d/nip.sh"
        print_status "Removed PATH configuration"
    fi

    # Remove library configuration
    if [ -f "/etc/ld.so.conf.d/nip.conf" ]; then
        rm -f "/etc/ld.so.conf.d/nip.conf"
        if command -v ldconfig >/dev/null 2>&1; then
            ldconfig 2>/dev/null || true
        fi
        print_status "Removed library configuration"
    fi
}

# Function to ask about data removal
ask_remove_data() {
    echo ""
    echo -e "${YELLOW}${WARN} Do you want to remove NIP data directories?${NC}"
    echo -e "${BLUE}${INFO} This includes:${NC}"
    echo -e "${BLUE}  • /Programs (installed packages)${NC}"
    echo -e "${BLUE}  • /System/Links (system links)${NC}"
    echo -e "${BLUE}  • /var/lib/nip (databases and generations)${NC}"
    echo -e "${BLUE}  • /var/cache/nip (package cache)${NC}"
    echo -e "${BLUE}  • /var/log/nip (log files)${NC}"
    echo -e "${BLUE}  • /etc/nip (configuration)${NC}"
    echo ""
    read -p "Remove all NIP data? [y/N]: " -r

    if [[ $REPLY =~ ^[Yy]$ ]]; then
        return 0  # Remove data
    else
        return 1  # Keep data
    fi
}

# Function to remove data directories
remove_data() {
    local dirs_to_remove=(
        "/Programs"
        "/System/Links"
        "/var/lib/nip"
        "/var/cache/nip"
        "/var/log/nip"
        "/etc/nip"
    )

    for dir in "${dirs_to_remove[@]}"; do
        if [ -d "$dir" ]; then
            rm -rf "$dir"
            print_status "Removed $dir"
        fi
    done
}

# Main uninstallation function
main() {
    echo -e "${TRASH} Starting NIP uninstallation..."
    echo ""

    # Pre-flight checks
    check_root
    confirm_uninstall

    # Remove binary
    remove_binary

    # Remove system integration
    remove_system_integration

    # Ask about data removal
    if ask_remove_data; then
        print_info "Removing NIP data directories..."
        remove_data
        print_status "All NIP data removed"
    else
        print_info "NIP data directories preserved"
        echo -e "${BLUE}${INFO} You can manually remove them later if needed${NC}"
    fi

    # Uninstallation complete
    echo ""
    echo -e "${GREEN}${CHECK} NIP uninstallation complete!${NC}"
    echo ""

    if ! ask_remove_data; then
        echo -e "${YELLOW}${INFO} To reinstall NIP later:${NC}"
        echo -e "${BLUE}  curl -L https://git.maiwald.work/Nexus/NexusToolKit/raw/branch/main/nip/install.sh | sudo bash${NC}"
        echo ""
    fi

    echo -e "${PURPLE}${SANTA} Thank you for using NIP! ✨${NC}"
}

# Handle command line arguments
case "${1:-}" in
    --help|-h)
        echo "NIP Uninstaller"
        echo ""
        echo "Usage:"
        echo "  curl -L https://git.maiwald.work/Nexus/NexusToolKit/raw/branch/main/nip/uninstall.sh | sudo bash"
        echo "  wget -O- https://git.maiwald.work/Nexus/NexusToolKit/raw/branch/main/nip/uninstall.sh | sudo bash"
        echo ""
        echo "Options:"
        echo "  --help, -h     Show this help message"
        echo ""
        exit 0
        ;;
    *)
        # Run main uninstallation
        main "$@"
        ;;
esac