#!/usr/bin/env zsh
# Configuration
MAINTENANCE_SCRIPT="$HOME/.local/bin/maintenance.sh"
TIMESTAMP_FILE="$HOME/.local/share/arch_maintenance_timestamp"
DISABLED_FILE="$HOME/.local/share/arch_maintenance_disabled"
INTERVAL=14  # Increased from 7 to 14 days
QUIET_MODE=${MAINTENANCE_QUIET:-false}  # Set MAINTENANCE_QUIET=true to disable

if [ ! -f "$MAINTENANCE_SCRIPT" ]; then
    echo "Maintenance script not found at $MAINTENANCE_SCRIPT"
    return 0
fi

# Check if maintenance is disabled
if [ -f "$DISABLED_FILE" ] || [ "$QUIET_MODE" = "true" ]; then
    return 0
fi

# Only run in interactive shells (not scripts)
if [[ ! -o interactive ]]; then
    return 0
fi

if [ -f "$TIMESTAMP_FILE" ]; then
    LAST_RUN=$(cat $TIMESTAMP_FILE)
    CURRENT_TIME=$(date +%s)
    DAYS_DIFF=$(( (CURRENT_TIME - LAST_RUN) / 86400 ))
    # Return immediately if maintenance not due
    [ $DAYS_DIFF -lt $INTERVAL ] && return 0
else
    # Create timestamp file if missing (first run)
    echo $(($(date +%s) - ${INTERVAL}*86400 - 1)) > $TIMESTAMP_FILE
    DAYS_DIFF=$INTERVAL  # Set for first run
fi

# Simple output functions that work with or without gum
print_styled() {
    if command -v gum &> /dev/null; then
        gum style "$@"
    else
        echo "$1"
    fi
}

ask_confirm() {
    if command -v gum &> /dev/null; then
        gum confirm "$1"
    else
        echo "$1 (y/n)"
        read -k 1 REPLY
        echo ""
        [[ $REPLY =~ ^[Yy]$ ]]
    fi
}

# Function to run maintenance script
run_maintenance() {
    print_styled "Running system maintenance script..."
    if sudo $MAINTENANCE_SCRIPT; then
        date +%s > $TIMESTAMP_FILE
        print_styled "✓ Maintenance completed successfully."
    else
        print_styled "✗ Maintenance script failed. Please check the output and run it manually."
    fi
}

# Function to snooze for 7 more days
snooze_maintenance() {
    SNOOZE_TIME=$(($(date +%s) - (${INTERVAL} - 7) * 86400))
    echo $SNOOZE_TIME > $TIMESTAMP_FILE
    print_styled "⏰ Maintenance reminder snoozed for 7 more days."
}

# Function to disable maintenance permanently
disable_maintenance() {
    touch "$DISABLED_FILE"
    print_styled "🔕 Maintenance reminders disabled permanently."
    print_styled "To re-enable: rm $DISABLED_FILE"
    print_styled "To run manually: sudo $MAINTENANCE_SCRIPT"
}

# Display maintenance notification
echo ""
print_styled "⚠️  It's been $DAYS_DIFF days since your last system maintenance"

# Enhanced confirmation with four options
if command -v gum &> /dev/null; then
    CHOICE=$(gum choose "Run maintenance now" "Skip this time" "Don't ask for 7 more days" "Disable permanently")
    case "$CHOICE" in
        "Run maintenance now")
            run_maintenance
            ;;
        "Skip this time")
            print_styled "Maintenance skipped. You'll be reminded next time you start your shell."
            print_styled "To run manually: sudo $MAINTENANCE_SCRIPT"
            ;;
        "Don't ask for 7 more days")
            snooze_maintenance
            ;;
        "Disable permanently")
            disable_maintenance
            ;;
    esac
else
    # Fallback for systems without gum
    echo "Choose an option:"
    echo "1) Run maintenance now"
    echo "2) Skip this time"
    echo "3) Don't ask for 7 more days"
    echo "4) Disable permanently"
    echo -n "Enter your choice (1-4): "
    read -k 1 CHOICE
    echo ""

    case "$CHOICE" in
        1)
            run_maintenance
            ;;
        2)
            print_styled "Maintenance skipped. You'll be reminded next time you start your shell."
            print_styled "To run manually: sudo $MAINTENANCE_SCRIPT"
            ;;
        3)
            snooze_maintenance
            ;;
        4)
            disable_maintenance
            ;;
        *)
            print_styled "Invalid choice. Maintenance skipped."
            print_styled "To run manually: sudo $MAINTENANCE_SCRIPT"
            ;;
    esac
fi

return 0
