#!/bin/bash

# LibrePortal WebUI App Configuration Manager
# Handles application configuration updates with atomic operations

# Function to update single config file atomically
updateAppConfig() {
    local app_name="$1"
    local config_content="$2"
    local config_file="$containers_dir/$app_name/$app_name.config"
    
    if [ -z "$app_name" ] || [ -z "$config_content" ]; then
        echo "❌ Error: app_name and config_content are required"
    fi
    
    echo "🔄 Updating config for $app_name atomically..."
    atomicWriteWebUI "$config_content" "$config_file"
    
    if [ $? -eq 0 ]; then
        echo "✅ Config updated successfully for $app_name"
        
        # Trigger web UI refresh if needed
        if [ -f "$containers_dir/frontend/data/last_update" ]; then
            date -Iseconds | runFileWrite "$containers_dir/frontend/data/last_update"
        fi
    else
        echo "❌ Failed to update config for $app_name"
    fi
}
