#!/bin/bash

set -e
set -o pipefail

# Get WC_ROOT (where this script and templates live)
WC_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
export WC_ROOT

# Set up cloud directory (WC_HOME is where user's cloud will be)
WC_HOME="$(pwd)"
export WC_HOME

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

# Helper functions
print_header() {
    echo -e "\n${BLUE}=== $1 ===${NC}\n"
}

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

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

print_success() {
    echo -e "${GREEN}SUCCESS:${NC} $1"
}

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

# Function to get current config value safely
get_current_config() {
    local key="$1"
    if [ -f "${WC_HOME}/config.yaml" ]; then
        set +e
        result=$(wild-config "${key}" 2>/dev/null)
        set -e
        echo "${result}"
    else
        echo ""
    fi
}

# Usage function
usage() {
    echo "Usage: wild-cluster-node-up <node-ip> [options]"
    echo ""
    echo "Apply Talos machine configuration to a registered node."
    echo ""
    echo "Arguments:"
    echo "  node-ip             IP address of the registered node"
    echo ""
    echo "Options:"
    echo "  -i, --insecure      Apply configuration in insecure mode (for maintenance mode nodes)"
    echo "  --dry-run           Show the command that would be executed without running it"
    echo "  -h, --help          Show this help message"
    echo ""
    echo "Examples:"
    echo "  wild-cluster-node-up 192.168.1.91"
    echo "  wild-cluster-node-up 192.168.1.100 --insecure"
    echo "  wild-cluster-node-up 192.168.1.100 --dry-run"
    echo ""
    echo "This script will:"
    echo "  - Verify the node is registered in config.yaml"
    echo "  - Check that a machine configuration exists for the node"
    echo "  - Apply the configuration using talosctl apply-config"
    echo "  - Use insecure mode for nodes in maintenance mode"
    echo ""
    echo "Requirements:"
    echo "  - Must be run from a wild-cloud directory"
    echo "  - Node must be registered (hardware detected) first"
    echo "  - Machine configuration must exist for the node"
}

# Parse arguments
NODE_IP=""
INSECURE_MODE=false
DRY_RUN=false

while [[ $# -gt 0 ]]; do
    case $1 in
        -i|--insecure)
            INSECURE_MODE=true
            shift
            ;;
        --dry-run)
            DRY_RUN=true
            shift
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        -*)
            echo "Unknown option $1"
            usage
            exit 1
            ;;
        *)
            if [ -z "$NODE_IP" ]; then
                NODE_IP="$1"
            else
                echo "Unexpected argument: $1"
                usage
                exit 1
            fi
            shift
            ;;
    esac
done

# Check if node IP was provided
if [ -z "$NODE_IP" ]; then
    echo "Error: Node IP address is required"
    usage
    exit 1
fi

# Check if we're in a wild-cloud directory
if [ ! -d ".wildcloud" ]; then
    print_error "You must run this script from a wild-cloud directory"
    print_info "Run 'wild-setup' or 'wild-init' first to initialize a wild-cloud project"
    exit 1
fi

# Check required configuration
if [ -z "$(get_current_config "cluster.name")" ]; then
    print_error "Basic cluster configuration is missing"
    print_info "Run 'wild-setup' or 'wild-init' first to configure your cluster"
    exit 1
fi

print_header "Talos Node Configuration Application"

# Check if the specified node is registered
NODE_INTERFACE=$(yq eval ".cluster.nodes.active.\"${NODE_IP}\".interface" "${WC_HOME}/config.yaml" 2>/dev/null)
NODE_DISK=$(yq eval ".cluster.nodes.active.\"${NODE_IP}\".disk" "${WC_HOME}/config.yaml" 2>/dev/null)
IS_CONTROL=$(yq eval ".cluster.nodes.active.\"${NODE_IP}\".control" "${WC_HOME}/config.yaml" 2>/dev/null)

if [ -z "$NODE_INTERFACE" ] || [ "$NODE_INTERFACE" = "null" ]; then
    print_error "Node $NODE_IP is not registered in config.yaml"
    print_info "Please register the node first by running:"
    print_info "  wild-node-detect $NODE_IP"
    print_info "Or run 'wild-setup' to register nodes interactively"
    exit 1
fi

# Determine node type
if [ "$IS_CONTROL" = "true" ]; then
    NODE_TYPE="control plane"
else
    NODE_TYPE="worker"
fi

print_info "Applying configuration to $NODE_TYPE node: $NODE_IP"
print_info "Node details:"
print_info "  - Interface: $NODE_INTERFACE"
print_info "  - Disk: $NODE_DISK"
print_info "  - Type: $NODE_TYPE"

# Check if machine config exists
NODE_SETUP_DIR="${WC_HOME}/setup/cluster-nodes"
CONFIG_FILE="${NODE_SETUP_DIR}/final/${NODE_IP}.yaml"

if [ ! -f "$CONFIG_FILE" ]; then
    print_error "Machine configuration not found: $CONFIG_FILE"
    print_info "Generate the machine configuration first:"
    print_info "  wild-cluster-node-machine-config-generate $NODE_IP"
    exit 1
fi

print_success "Found machine configuration: $CONFIG_FILE"

# Build talosctl command
TALOSCTL_CMD="talosctl apply-config"

if [ "$INSECURE_MODE" = true ]; then
    TALOSCTL_CMD="$TALOSCTL_CMD --insecure"
    print_info "Using insecure mode (for maintenance mode nodes)"
fi

TALOSCTL_CMD="$TALOSCTL_CMD --nodes $NODE_IP --file $CONFIG_FILE"

# Show the command
echo ""
print_info "Command to execute:"
echo "  $TALOSCTL_CMD"
echo ""

if [ "$DRY_RUN" = true ]; then
    print_info "Dry run mode - command shown above but not executed"
    exit 0
fi

# Apply the configuration
print_info "Applying machine configuration..."
echo ""

if eval "$TALOSCTL_CMD"; then
    print_success "Machine configuration applied successfully!"
    echo ""
    
    if [ "$IS_CONTROL" = "true" ]; then
        print_info "Next steps for control plane node:"
        echo "  1. Wait for the node to reboot and come up with the new configuration"
        echo "  2. If this is your first control plane node, bootstrap it:"
        echo "     talosctl bootstrap --nodes $NODE_IP"
        echo "  3. Get kubeconfig when cluster is ready:"
        echo "     talosctl kubeconfig"
    else
        print_info "Next steps for worker node:"
        echo "  1. Wait for the node to reboot and come up with the new configuration"
        echo "  2. Node will join the cluster automatically"
        echo "  3. Verify the node appears in the cluster:"
        echo "     kubectl get nodes"
    fi
    
    echo ""
    print_info "Monitor node status with:"
    echo "  talosctl --nodes $NODE_IP dmesg"
    echo "  talosctl --nodes $NODE_IP get members"
    
else
    print_error "Failed to apply machine configuration"
    echo ""
    print_info "Troubleshooting tips:"
    echo "  - Ensure the node is accessible at $NODE_IP"
    echo "  - For nodes in maintenance mode, use --insecure flag"
    echo "  - Check network connectivity and firewall settings"
    echo "  - Verify the machine configuration file is valid"
    exit 1
fi

print_success "Node configuration completed!"