#!/bin/bash

# AutoNAS Network Handler - System integration for network interface events
# This script is called by udev when network interfaces change state
# Uses autonas-core.sh for all business logic

# Load the AutoNAS core library
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/autonas-core.sh" || {
    echo "Error: Cannot load AutoNAS core library" >&2
    exit 1
}

# Set LOG_TAG for this component
LOG_TAG="autonas-network-handler"

# Function to handle interface coming up
handle_interface_up() {
    local interface="$1"
    local event_type="$2"
    
    log_message "Interface up event: $interface ($event_type)" "info"
    
    # Check if this interface is used by AutoNAS
    if interface_has_autonas_config "$interface"; then
        log_message "Interface $interface is configured for AutoNAS, restoring IP configurations" "info"
        
        # Small delay to let interface stabilize completely
        sleep 3
        
        # Restore IPs for this interface
        if restore_interface_ips "$interface"; then
            log_message "Successfully processed interface $interface restoration" "info"
        else
            log_message "Failed to fully process interface $interface restoration" "warning"
        fi
    else
        log_message "Interface $interface is not configured for AutoNAS, ignoring" "debug"
    fi
}

# Function to handle interface going down
handle_interface_down() {
    local interface="$1"
    local event_type="$2"
    
    log_message "Interface down event: $interface ($event_type)" "info"
    
    if interface_has_autonas_config "$interface"; then
        log_message "AutoNAS-configured interface $interface went down" "warning"
    fi
}

# Main script logic
ACTION="$1"
INTERFACE="$2"
EXTRA="$3"

case "$ACTION" in
    "interface_up"|"usb_interface_up")
        if [ -n "$INTERFACE" ]; then
            # Run in background to avoid udev timeout, but with shorter delay for faster response
            (
                sleep 2  # Reduced from 3 seconds for faster response
                handle_interface_up "$INTERFACE" "$ACTION"
            ) &
            log_message "Started background handler for interface up: $INTERFACE (PID: $!)" "debug"
        fi
        ;;
    "interface_down"|"usb_interface_down")
        if [ -n "$INTERFACE" ]; then
            handle_interface_down "$INTERFACE" "$ACTION"
        fi
        ;;
    "interface_change")
        if [ -n "$INTERFACE" ] && [ -n "$EXTRA" ]; then
            case "$EXTRA" in
                "up"|"carrier_up")
                    (
                        sleep 2
                        handle_interface_up "$INTERFACE" "change_to_$EXTRA"
                    ) &
                    log_message "Started background handler for interface $EXTRA: $INTERFACE (PID: $!)" "debug"
                    ;;
                "down"|"carrier_down")
                    handle_interface_down "$INTERFACE" "change_to_$EXTRA"
                    ;;
                *)
                    log_message "Unknown interface change state: $EXTRA for interface $INTERFACE" "warning"
                    ;;
            esac
        fi
        ;;
    *)
        log_message "Unknown action: $ACTION with interface: $INTERFACE" "warning"
        exit 1
        ;;
esac

# Exit immediately so udev doesn't wait
exit 0
