#!/bin/bash

set -e
set -o pipefail

UPDATE=false

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --update)
            UPDATE=true
            shift
            ;;
        -h|--help)
            echo "Usage: $0 <app_name> [--update]"
            echo ""
            echo "Configure an app by applying templates and merging configuration."
            echo ""
            echo "Options:"
            echo "  --update    Overwrite existing app files without confirmation"
            echo "  -h, --help  Show this help message"
            exit 0
            ;;
        -*)
            echo "Unknown option $1"
            echo "Usage: $0 <app_name> [--update]"
            exit 1
            ;;
        *)
            if [ -z "${APP_NAME}" ]; then
                APP_NAME="$1"
            else
                echo "Too many arguments"
                echo "Usage: $0 <app_name> [--update]"
                exit 1
            fi
            shift
            ;;
    esac
done

if [ -z "${APP_NAME}" ]; then
    echo "Usage: $0 <app_name> [--update]"
    exit 1
fi

# Initialize Wild Cloud environment
if [ -z "${WC_ROOT}" ]; then
    print "WC_ROOT is not set."
    exit 1
else
    source "${WC_ROOT}/scripts/common.sh"
    init_wild_env
fi

CONFIG_FILE="${WC_HOME}/config.yaml"
if [ ! -f "${CONFIG_FILE}" ]; then
    echo "Creating config file at ${CONFIG_FILE}"
    echo "# Wild Cloud Configuration" > "${CONFIG_FILE}"
    echo "# This file contains app configurations and should be committed to git" >> "${CONFIG_FILE}"
    echo "" >> "${CONFIG_FILE}"
fi

SECRETS_FILE="${WC_HOME}/secrets.yaml"
if [ ! -f "${SECRETS_FILE}" ]; then
    echo "Creating secrets file at ${SECRETS_FILE}"
    echo "# Wild Cloud Secrets Configuration" > "${SECRETS_FILE}"
    echo "# This file contains sensitive data and should NOT be committed to git" >> "${SECRETS_FILE}"
    echo "# Add this file to your .gitignore" >> "${SECRETS_FILE}"
    echo "" >> "${SECRETS_FILE}"
fi

# Check if app is cached, if not fetch it first
CACHE_APP_DIR="${WC_HOME}/.wildcloud/cache/apps/${APP_NAME}"
if [ ! -d "${CACHE_APP_DIR}" ]; then
    echo "Cache directory for app '${APP_NAME}' not found at ${CACHE_APP_DIR}"
    echo "Please fetch the app first using 'wild-app-fetch ${APP_NAME}'"
    exit 1
fi
if [ ! -d "${CACHE_APP_DIR}" ]; then
    echo "App '${APP_NAME}' not found in cache, fetching..."
    if [ "${UPDATE}" = true ]; then
        ./bin/wild-app-fetch "${APP_NAME}" --update
    else
        ./bin/wild-app-fetch "${APP_NAME}"
    fi
fi

APPS_DIR="${WC_HOME}/apps"
if [ ! -d "${APPS_DIR}" ]; then
    echo "Creating apps directory at ${APPS_DIR}"
    mkdir -p "${APPS_DIR}"
fi

DEST_APP_DIR="${WC_HOME}/apps/${APP_NAME}"
if [ -d "${DEST_APP_DIR}" ]; then
    if [ "${UPDATE}" = true ]; then
        echo "Updating app '${APP_NAME}'"
        rm -rf "${DEST_APP_DIR}"
    else
        echo "Warning: Destination directory ${DEST_APP_DIR} already exists"
        read -p "Do you want to overwrite it? (y/N): " -n 1 -r
        echo
        if [[ ! $REPLY =~ ^[Yy]$ ]]; then
            echo "Configuration cancelled"
            exit 1
        fi
        rm -rf "${DEST_APP_DIR}"
    fi
fi
mkdir -p "${DEST_APP_DIR}"

echo "Adding app '${APP_NAME}' from cache to ${DEST_APP_DIR}"

# Step 1: Copy only manifest.yaml from cache first
MANIFEST_FILE="${CACHE_APP_DIR}/manifest.yaml"
if [ -f "${MANIFEST_FILE}" ]; then
    echo "Copying manifest.yaml from cache"
    cp "${MANIFEST_FILE}" "${DEST_APP_DIR}/manifest.yaml"
else
    echo "Warning: manifest.yaml not found in cache for app '${APP_NAME}'"
    exit 1
fi

# Step 2: Add missing config and secret values based on manifest
echo "Processing configuration and secrets from manifest.yaml"

# Check if the app section exists in config.yaml, if not create it
if ! yq eval ".apps.${APP_NAME}" "${CONFIG_FILE}" >/dev/null 2>&1; then
    yq eval ".apps.${APP_NAME} = {}" -i "${CONFIG_FILE}"
fi

# Extract defaultConfig from manifest.yaml and merge into config.yaml
if yq eval '.defaultConfig' "${DEST_APP_DIR}/manifest.yaml" | grep -q -v '^null$'; then
    echo "Merging defaultConfig from manifest.yaml into .wildcloud/config.yaml"
    
    # Check if the app config already exists
    if yq eval ".apps.${APP_NAME}" "${CONFIG_FILE}" | grep -q '^null$'; then
        yq eval ".apps.${APP_NAME} = {}" -i "${CONFIG_FILE}"
    fi
    
    # Merge defaultConfig into the app config, preserving nested structure
    # This preserves the nested structure for objects like resources.requests.memory
    temp_manifest=$(mktemp)
    yq eval '.defaultConfig' "${DEST_APP_DIR}/manifest.yaml" > "$temp_manifest"
    yq eval ".apps.${APP_NAME} = (.apps.${APP_NAME} // {}) * load(\"$temp_manifest\")" -i "${CONFIG_FILE}"
    rm "$temp_manifest"
    
    # Process template variables in the merged config
    echo "Processing template variables in app config"
    temp_config=$(mktemp)
    
    # Build gomplate command with config context
    gomplate_cmd="gomplate -c .=${CONFIG_FILE}"
    
    # Add secrets context if secrets.yaml exists
    if [ -f "${SECRETS_FILE}" ]; then
        gomplate_cmd="${gomplate_cmd} -c secrets=${SECRETS_FILE}"
    fi
    
    # Process the entire config file through gomplate to resolve template variables
    ${gomplate_cmd} -f "${CONFIG_FILE}" > "$temp_config"
    mv "$temp_config" "${CONFIG_FILE}"
    
    echo "Merged defaultConfig for app '${APP_NAME}'"
fi

# Scaffold required secrets into .wildcloud/secrets.yaml if they don't exist
if yq eval '.requiredSecrets' "${DEST_APP_DIR}/manifest.yaml" | grep -q -v '^null$'; then
    echo "Scaffolding required secrets for app '${APP_NAME}'"
    
    # Ensure .wildcloud/secrets.yaml exists
    if [ ! -f "${SECRETS_FILE}" ]; then
        echo "# Wild Cloud Secrets Configuration" > "${SECRETS_FILE}"
        echo "# This file contains sensitive data and should NOT be committed to git" >> "${SECRETS_FILE}"
        echo "# Add this file to your .gitignore" >> "${SECRETS_FILE}"
        echo "" >> "${SECRETS_FILE}"
    fi
    
    # Check if apps section exists, if not create it
    if ! yq eval ".apps" "${SECRETS_FILE}" >/dev/null 2>&1; then
        yq eval ".apps = {}" -i "${SECRETS_FILE}"
    fi
    
    # Check if app section exists, if not create it
    if ! yq eval ".apps.${APP_NAME}" "${SECRETS_FILE}" >/dev/null 2>&1; then
        yq eval ".apps.${APP_NAME} = {}" -i "${SECRETS_FILE}"
    fi
    
    # Add dummy values for each required secret if not already present
    yq eval '.requiredSecrets[]' "${DEST_APP_DIR}/manifest.yaml" | while read -r secret_path; do
        current_value=$(yq eval ".${secret_path} // \"null\"" "${SECRETS_FILE}")
        
        if [ "${current_value}" = "null" ]; then
            echo "Adding random secret: ${secret_path}"
            random_secret=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 6)
            yq eval ".${secret_path} = \"${random_secret}\"" -i "${SECRETS_FILE}"
        fi
    done
    
    echo "Required secrets scaffolded for app '${APP_NAME}'"
fi

# Step 3: Copy and compile all other files from cache to app directory
echo "Copying and compiling remaining files from cache"

# Function to process a file with gomplate if it's a YAML file
process_file() {
    local src_file="$1"
    local dest_file="$2"
    
    echo "Processing file: ${dest_file}"
    
    # Build gomplate command with config context (enables .config shorthand)
    gomplate_cmd="gomplate -c .=${CONFIG_FILE}"
    
    # Add secrets context if secrets.yaml exists (enables .secrets shorthand)
    if [ -f "${SECRETS_FILE}" ]; then
        gomplate_cmd="${gomplate_cmd} -c secrets=${SECRETS_FILE}"
    fi
    
    # Execute gomplate with the file
    ${gomplate_cmd} -f "${src_file}" > "${dest_file}"
}

# Copy directory structure and process files (excluding manifest.yaml which was already copied)
find "${CACHE_APP_DIR}" -type d | while read -r src_dir; do
    rel_path="${src_dir#${CACHE_APP_DIR}}"
    rel_path="${rel_path#/}"  # Remove leading slash if present
    if [ -n "${rel_path}" ]; then
        mkdir -p "${DEST_APP_DIR}/${rel_path}"
    fi
done

find "${CACHE_APP_DIR}" -type f | while read -r src_file; do
    rel_path="${src_file#${CACHE_APP_DIR}}"
    rel_path="${rel_path#/}"  # Remove leading slash if present
    
    # Skip manifest.yaml since it was already copied in step 1
    if [ "${rel_path}" = "manifest.yaml" ]; then
        continue
    fi
    
    dest_file="${DEST_APP_DIR}/${rel_path}"
    
    # Ensure destination directory exists
    dest_dir=$(dirname "${dest_file}")
    mkdir -p "${dest_dir}"
    
    process_file "${src_file}" "${dest_file}"
done

echo "Successfully added app '${APP_NAME}' with template processing"