#!/bin/bash

checkConfigFilesMissingFiles()
{
    # Simply check if the configs directory exists and has some content
    if [ ! -d "$configs_dir" ]; then
        isError "Configs directory not found at $configs_dir"
        return 1
    fi

    local missing_files_count=0
    local found_files_count=0

    # Walk every category the install templates define (configs/<category>/),
    # derived from the template tree itself — so a new category, or one a
    # hardcoded list would miss (webui/backup/security were), heals onto
    # existing installs automatically.
    local install_category_dir category target_category_dir
    for install_category_dir in "$install_configs_dir"*/; do
        [ -d "$install_category_dir" ] || continue
        category="$(basename "$install_category_dir")"
        target_category_dir="${configs_dir%/}/$category"

        # Create target category directory if it doesn't exist
        if [ ! -d "$target_category_dir" ]; then
            runInstallOp mkdir -p "$target_category_dir"
        fi

        # Check each file in the install category directory
        local install_file
        for install_file in "$install_category_dir"*; do
            if [ -f "$install_file" ] && [[ ! "$install_file" =~ \.category$ ]]; then
                local filename=$(basename "$install_file")
                local target_file="$target_category_dir/$filename"

                if [ ! -f "$target_file" ]; then
                    # Copy missing file from install directory
                    runInstallOp cp "$install_file" "$target_file"
                    ((missing_files_count++))
                fi
                ((found_files_count++))
            fi
        done

        # Heal the category's .category metadata too: copy it when absent (a
        # brand-new category is invisible to the WebUI Config editor without
        # it), and when present merge in any template SUBCATEGORY_ORDER entries
        # it lacks — the editor only renders files listed there, so a healed
        # config file would otherwise ship hidden. User-kept order and any
        # extra local entries are preserved; new entries append.
        local tmpl_cat="${install_category_dir}.category"
        local live_cat="$target_category_dir/.category"
        if [ -f "$tmpl_cat" ]; then
            if [ ! -f "$live_cat" ]; then
                runInstallOp cp "$tmpl_cat" "$live_cat"
            else
                local tmpl_order live_order merged entry _entries
                tmpl_order="$(grep -m1 '^SUBCATEGORY_ORDER=' "$tmpl_cat" 2>/dev/null | cut -d= -f2-)"
                live_order="$(runInstallOp grep -m1 '^SUBCATEGORY_ORDER=' "$live_cat" 2>/dev/null | cut -d= -f2-)"
                merged="$live_order"
                IFS=',' read -ra _entries <<< "$tmpl_order"
                for entry in "${_entries[@]}"; do
                    [ -n "$entry" ] || continue
                    [[ ",$merged," == *",$entry,"* ]] || merged="${merged:+$merged,}$entry"
                done
                if [ -n "$merged" ] && [ "$merged" != "$live_order" ]; then
                    if runInstallOp grep -q '^SUBCATEGORY_ORDER=' "$live_cat" 2>/dev/null; then
                        runInstallOp sed -i "s/^SUBCATEGORY_ORDER=.*/SUBCATEGORY_ORDER=$merged/" "$live_cat"
                    else
                        printf 'SUBCATEGORY_ORDER=%s\n' "$merged" | runInstallOp tee -a "$live_cat" >/dev/null
                    fi
                fi
            fi
        fi
    done

    if [ "$found_files_count" -gt 0 ]; then
        if [ "$missing_files_count" -gt 0 ]; then
            isSuccessful "$missing_files_count config files were missing and have been added to the configs folder."
        else
            isSuccessful "All config files are successfully set up in the configs folder."
        fi
    else
        isNotice "No configuration files found in install directory. This may be a fresh installation."
        exit
    fi
}
