#!/bin/bash
source "$(dirname "${BASH_SOURCE[0]}")/env-init.sh"

echo "DEBUG: Scanning directory: $ROOT/apps/"

# Check if the apps directory actually exists
if [ ! -d "$ROOT/apps/" ]; then
    echo "ERROR: Directory $ROOT/apps/ does not exist!"
    exit 1
fi

for app in "$ROOT/apps/"*; do
    [ -d "$app" ] || continue
    APP_NAME=$(basename "$app")
    echo "Validating App: $APP_NAME..."
    
    # Define your folder requirements
    REQUIRED=("admin" "css" "data" "data/sql" "docs" "images" "js" "php")
    
    for folder in "${REQUIRED[@]}"; do
        if [ ! -d "$app/$folder" ]; then
            echo "  -> Missing directory: $folder. Creating it..."
            mkdir -p "$app/$folder"
        fi
    done
    
    # Auto-generate manifest if missing
    if [ ! -f "$app/data/manifest.json" ]; then
        echo "  -> Generating missing manifest.json..."
        echo '{"app_name": "'$APP_NAME'", "db_required": false}' > "$app/data/manifest.json"
    fi
done
echo "Validation complete."
