#!/usr/bin/env zsh
# Configuration
MAINTENANCE_SCRIPT="$HOME/.local/bin/maintenance.sh"
TIMESTAMP_FILE="$HOME/.local/share/arch_maintenance_timestamp"
INTERVAL=7

if [ ! -f "$MAINTENANCE_SCRIPT" ]; then
    echo "Maintenance script not found at $MAINTENANCE_SCRIPT"
    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."
}

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

# Enhanced confirmation with three options
if command -v gum &> /dev/null; then
    CHOICE=$(gum choose "Run maintenance now" "Skip this time" "Don't ask for 7 more days")
    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
            ;;
    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 -n "Enter your choice (1-3): "
    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
            ;;
        *)
            print_styled "Invalid choice. Maintenance skipped."
            print_styled "To run manually: sudo $MAINTENANCE_SCRIPT"
            ;;
    esac
fi

return 0
