#!/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
else
    echo "Adding app '${APP_NAME}' to '${DEST_APP_DIR}'."
fi
mkdir -p "${DEST_APP_DIR}"

# Step 1: Copy only manifest.yaml from cache first
MANIFEST_FILE="${CACHE_APP_DIR}/manifest.yaml"
if [ -f "${MANIFEST_FILE}" ]; then
    # manifest.yaml is allowed to have gomplate variables in the defaultConfig and requiredSecrets sections.
    # We need to use gomplate to process these variables before using yq.
    echo "Copying app manifest from cache."
    DEST_MANIFEST="${DEST_APP_DIR}/manifest.yaml"
    if [ -f "${SECRETS_FILE}" ]; then
        gomplate_cmd="gomplate -c .=${CONFIG_FILE} -c secrets=${SECRETS_FILE} -f ${MANIFEST_FILE} -o ${DEST_MANIFEST}"
    else
        gomplate_cmd="gomplate -c .=${CONFIG_FILE} -f ${MANIFEST_FILE} -o ${DEST_MANIFEST}"
    fi
    if ! eval "${gomplate_cmd}"; then
        echo "Error processing manifest.yaml with gomplate"
        exit 1
    fi
else
    echo "Warning: App manifest not found in cache."
    exit 1
fi

# 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

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

# Merge processed defaultConfig into the app config, preserving existing values
# This preserves existing configuration values while adding missing defaults
if yq eval '.defaultConfig' "${DEST_MANIFEST}" | grep -q -v '^null$'; then
    
    # Extract defaultConfig from the processed manifest and merge with existing app config
    # The * operator merges objects, with the right side taking precedence for conflicting keys
    # So (.apps.${APP_NAME} // {}) preserves existing values, defaultConfig adds missing ones
    temp_default_config=$(mktemp)
    yq eval '.defaultConfig' "${DEST_MANIFEST}" > "$temp_default_config"
    yq eval ".apps.${APP_NAME} = load(\"$temp_default_config\") * (.apps.${APP_NAME} // {})" -i "${CONFIG_FILE}"
    rm "$temp_default_config"
    
    echo "Merged default configuration from app manifest into '${CONFIG_FILE}'."
    # Remove defaultConfig from the copied manifest since it's now in config.yaml.
    yq eval 'del(.defaultConfig)' -i "${DEST_MANIFEST}" 
fi

# Scaffold required secrets into .wildcloud/secrets.yaml if they don't exist
if yq eval '.requiredSecrets' "${DEST_MANIFEST}" | grep -q -v '^null$'; then
    
    # Ensure .wildcloud/secrets.yaml exists
    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
    
    # Add random values for each required secret if not already present
    while read -r secret_path; do
        current_value=$(yq eval ".${secret_path} // \"null\"" "${SECRETS_FILE}")
        
        if [ "${current_value}" = "null" ]; then
            random_secret=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-32)
            yq eval ".${secret_path} = \"${random_secret}\"" -i "${SECRETS_FILE}"
        fi
    done < <(yq eval '.requiredSecrets[]' "${DEST_MANIFEST}")
    echo "Required secrets declared in app manifest added to '${SECRETS_FILE}'."
fi

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

cp -r "${CACHE_APP_DIR}/." "${DEST_APP_DIR}/"
find "${DEST_APP_DIR}" -type f | while read -r dest_file; do
    rel_path="${dest_file#${DEST_APP_DIR}/}"
    
    if [ "${rel_path}" = "manifest.yaml" ]; then
        continue
    fi
    
    temp_file=$(mktemp)
    gomplate -c .=${CONFIG_FILE} -c secrets=${SECRETS_FILE} -f "${dest_file}" > "${temp_file}"
    mv "${temp_file}" "${dest_file}"
done

echo "Added '${APP_NAME}'."
