#!/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 prompt for input with default value
prompt_with_default() {
    local prompt="$1"
    local default="$2"
    local current_value="$3"
    local result
    
    if [ -n "${current_value}" ] && [ "${current_value}" != "null" ]; then
        printf "%s [current: %s]: " "${prompt}" "${current_value}" >&2
        read -r result
        if [ -z "${result}" ]; then
            result="${current_value}"
        fi
    elif [ -n "${default}" ]; then
        printf "%s [default: %s]: " "${prompt}" "${default}" >&2
        read -r result
        if [ -z "${result}" ]; then
            result="${default}"
        fi
    else
        printf "%s: " "${prompt}" >&2
        read -r result
        while [ -z "${result}" ]; do
            printf "This value is required. Please enter a value: " >&2
            read -r result
        done
    fi
    
    echo "${result}"
}

# 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
}

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

# Usage function
usage() {
    echo "Usage: wild-cluster-node-image-create [options]"
    echo ""
    echo "Generate custom Talos installer image URLs for cluster nodes."
    echo ""
    echo "Options:"
    echo "  -h, --help          Show this help message"
    echo ""
    echo "This script will:"
    echo "  - Configure basic cluster settings if needed"
    echo "  - Generate custom Talos installer image URL"
    echo "  - Display the installer URL for PXE boot or ISO creation"
    echo ""
    echo "Requirements:"
    echo "  - Must be run from a wild-cloud directory"
    echo "  - Requires Talos version and schematic ID configuration"
}

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--help)
            usage
            exit 0
            ;;
        -*)
            echo "Unknown option $1"
            usage
            exit 1
            ;;
        *)
            echo "Unexpected argument: $1"
            usage
            exit 1
            ;;
    esac
done

# 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

# Configure basic settings if needed
if [ ! -f "${WC_HOME}/config.yaml" ] || [ -z "$(get_current_config "operator.email")" ]; then
    print_header "Basic Configuration"
    
    # Detect current network for suggestions
    CURRENT_IP=$(ip route get 8.8.8.8 | awk '{print $7; exit}' 2>/dev/null || echo "192.168.1.100")
    GATEWAY_IP=$(ip route | grep default | awk '{print $3; exit}' 2>/dev/null || echo "192.168.1.1")
    SUBNET_PREFIX=$(echo "${CURRENT_IP}" | cut -d. -f1-3)
    print_info "Detected network: ${SUBNET_PREFIX}.x (gateway: ${GATEWAY_IP})"
    
    echo "This will configure basic settings for your wild-cloud deployment."
    echo ""
    
    # Basic Information
    current_email=$(get_current_config "operator.email")
    email=$(prompt_with_default "Your email address (for Let's Encrypt certificates)" "" "${current_email}")
    wild-config-set "operator.email" "${email}"

    # Domain Configuration
    current_base_domain=$(get_current_config "cloud.baseDomain")
    base_domain=$(prompt_with_default "Your base domain name (e.g., example.com)" "" "${current_base_domain}")
    wild-config-set "cloud.baseDomain" "${base_domain}"

    current_domain=$(get_current_config "cloud.domain")
    domain=$(prompt_with_default "Your public cloud domain" "cloud.${base_domain}" "${current_domain}")
    wild-config-set "cloud.domain" "${domain}"

    current_internal_domain=$(get_current_config "cloud.internalDomain")
    internal_domain=$(prompt_with_default "Your internal cloud domain" "internal.${domain}" "${current_internal_domain}")
    wild-config-set "cloud.internalDomain" "${internal_domain}"

    # Derive cluster name from domain
    cluster_name=$(echo "${domain}" | tr '.' '-' | tr '[:upper:]' '[:lower:]')
    wild-config-set "cluster.name" "${cluster_name}"
    print_info "Set cluster name to: ${cluster_name}"
    
    print_success "Basic configuration completed"
    echo ""
fi

# Configure cluster settings if needed
if [ -z "$(get_current_config "cluster.nodes.talos.version")" ] || [ -z "$(get_current_config "cluster.nodes.talos.schematicId")" ]; then
    print_header "Kubernetes Cluster Configuration"
    
    current_talos_version=$(get_current_config "cluster.nodes.talos.version")
    talos_version=$(prompt_with_default "Talos version" "v1.6.1" "${current_talos_version}")
    wild-config-set "cluster.nodes.talos.version" "${talos_version}"

    # Talos schematic ID
    current_schematic_id=$(get_current_config "cluster.nodes.talos.schematicId")
    echo ""
    print_info "Get your Talos schematic ID from: https://factory.talos.dev/"
    print_info "This customizes Talos with the drivers needed for your hardware."
    
    # Look up default schematic ID from talos-schemas.yaml
    default_schematic_id=""
    schemas_file="${WC_ROOT}/setup/cluster-nodes/talos-schemas.yaml"
    if [ -f "$schemas_file" ]; then
        default_schematic_id=$(yq eval ".talos-schemas.\"${talos_version}\"" "$schemas_file" 2>/dev/null)
        if [ -n "$default_schematic_id" ] && [ "$default_schematic_id" != "null" ]; then
            print_info "Default schematic ID available for Talos $talos_version"
        else
            default_schematic_id=""
        fi
    fi
    
    schematic_id=$(prompt_with_default "Talos schematic ID" "${default_schematic_id}" "${current_schematic_id}")
    wild-config-set "cluster.nodes.talos.schematicId" "${schematic_id}"
    
    print_success "Cluster configuration completed"
    echo ""
fi

# =============================================================================
# INSTALLER IMAGE GENERATION AND ASSET DOWNLOADING
# =============================================================================

print_header "Talos Installer Image Generation and Asset Download"

# Get Talos version and schematic ID from config
TALOS_VERSION=$(get_current_config cluster.nodes.talos.version)
SCHEMATIC_ID=$(get_current_config cluster.nodes.talos.schematicId)

print_info "Creating custom Talos installer image..."
print_info "Talos version: $TALOS_VERSION"

# Check if schematic ID exists
if [ -z "$SCHEMATIC_ID" ] || [ "$SCHEMATIC_ID" = "null" ]; then
    print_error "No schematic ID found in config.yaml"
    print_info "You can get a schematic ID from: https://factory.talos.dev/"
    
    # Look up default schematic ID from talos-schemas.yaml
    fallback_default=""
    schemas_file="${WC_ROOT}/setup/cluster-nodes/talos-schemas.yaml"
    if [ -f "$schemas_file" ]; then
        fallback_default=$(yq eval ".talos-schemas.\"${TALOS_VERSION}\"" "$schemas_file" 2>/dev/null)
        if [ -n "$fallback_default" ] && [ "$fallback_default" != "null" ]; then
            print_info "Default schematic ID available for Talos $TALOS_VERSION"
            read -p "Enter schematic ID [$fallback_default]: " -r SCHEMATIC_ID
            if [ -z "$SCHEMATIC_ID" ]; then
                SCHEMATIC_ID="$fallback_default"
            fi
        else
            read -p "Enter schematic ID: " -r SCHEMATIC_ID
        fi
    else
        read -p "Enter schematic ID: " -r SCHEMATIC_ID
    fi
    
    if [ -n "$SCHEMATIC_ID" ]; then
        wild-config-set "cluster.nodes.talos.schematicId" "$SCHEMATIC_ID"
    else
        print_error "Schematic ID required for installer image generation"
        exit 1
    fi
fi

print_info "Schematic ID: $SCHEMATIC_ID"

if [ -f "${WC_HOME}/config.yaml" ] && yq eval '.cluster.nodes.talos.schematic.customization.systemExtensions.officialExtensions' "${WC_HOME}/config.yaml" >/dev/null 2>&1; then
    echo ""
    print_info "Schematic includes:"
    yq eval '.cluster.nodes.talos.schematic.customization.systemExtensions.officialExtensions[]' "${WC_HOME}/config.yaml" | sed 's/^/  - /' || true
    echo ""
fi

# Generate installer image URL
INSTALLER_URL="factory.talos.dev/metal-installer/$SCHEMATIC_ID:$TALOS_VERSION"

print_success "Custom installer image URL generated!"
echo ""
print_info "Installer URL: $INSTALLER_URL"

# =============================================================================
# ASSET DOWNLOADING AND CACHING
# =============================================================================

print_header "Downloading and Caching PXE Boot Assets"

# Create cache directories
CACHE_DIR="${WC_HOME}/.wildcloud"
PXE_CACHE_DIR="${CACHE_DIR}/pxe"
IPXE_CACHE_DIR="${CACHE_DIR}/ipxe"
mkdir -p "$PXE_CACHE_DIR/amd64"
mkdir -p "$IPXE_CACHE_DIR"

# Download Talos kernel and initramfs for PXE boot
print_info "Downloading Talos PXE assets..."
KERNEL_URL="https://pxe.factory.talos.dev/image/${SCHEMATIC_ID}/${TALOS_VERSION}/kernel-amd64"
INITRAMFS_URL="https://pxe.factory.talos.dev/image/${SCHEMATIC_ID}/${TALOS_VERSION}/initramfs-amd64.xz"

KERNEL_PATH="${PXE_CACHE_DIR}/amd64/vmlinuz"
INITRAMFS_PATH="${PXE_CACHE_DIR}/amd64/initramfs.xz"

# Function to download with progress
download_asset() {
    local url="$1"
    local path="$2"
    local description="$3"
    
    if [ -f "$path" ]; then
        print_info "$description already cached at $path"
        return 0
    fi
    
    print_info "Downloading $description..."
    print_info "URL: $url"
    
    if command -v wget >/dev/null 2>&1; then
        wget --progress=bar:force -O "$path" "$url"
    elif command -v curl >/dev/null 2>&1; then
        curl -L --progress-bar -o "$path" "$url"
    else
        print_error "Neither wget nor curl is available for downloading"
        return 1
    fi
    
    # Verify download
    if [ ! -f "$path" ] || [ ! -s "$path" ]; then
        print_error "Download failed or file is empty: $path"
        rm -f "$path"
        return 1
    fi
    
    print_success "$description downloaded successfully"
}

# Download Talos PXE assets
download_asset "$KERNEL_URL" "$KERNEL_PATH" "Talos kernel"
download_asset "$INITRAMFS_URL" "$INITRAMFS_PATH" "Talos initramfs"

# Download iPXE bootloader files
print_info "Downloading iPXE bootloader assets..."
download_asset "http://boot.ipxe.org/ipxe.efi" "${IPXE_CACHE_DIR}/ipxe.efi" "iPXE EFI bootloader"
download_asset "http://boot.ipxe.org/undionly.kpxe" "${IPXE_CACHE_DIR}/undionly.kpxe" "iPXE BIOS bootloader"
download_asset "http://boot.ipxe.org/arm64-efi/ipxe.efi" "${IPXE_CACHE_DIR}/ipxe-arm64.efi" "iPXE ARM64 EFI bootloader"

echo ""
print_success "All assets downloaded and cached!"
echo ""
print_info "Cached assets:"
echo "  Talos kernel:     $KERNEL_PATH"
echo "  Talos initramfs:  $INITRAMFS_PATH"
echo "  iPXE EFI:         ${IPXE_CACHE_DIR}/ipxe.efi"
echo "  iPXE BIOS:        ${IPXE_CACHE_DIR}/undionly.kpxe"
echo "  iPXE ARM64:       ${IPXE_CACHE_DIR}/ipxe-arm64.efi"
echo ""
print_info "Use this URL for:"
echo "  - PXE boot configuration (update boot.ipxe kernel line)"
echo "  - ISO creation: curl -LO https://$INSTALLER_URL"
echo "  - USB creation: dd if=talos-installer.iso of=/dev/sdX"
echo ""
print_success "Installer image generation and asset caching completed!"