#!/bin/bash

# Define the path to your JSON file
JSON_FILE="workingOn.json"

# --- Configuration for Dry Run Mode ---
DRY_RUN=false
VERBOSE_OUTPUT=false

# Parse command-line arguments
for arg in "$@"
do
    case $arg in
        --list-only|-l)
        DRY_RUN=true
        echo "Dry Run Mode: Will list files and directories but NOT modify $JSON_FILE."
        shift # Remove --list-only from processing
        ;;
        --verbose|-v)
        VERBOSE_OUTPUT=true
        echo "Verbose Output Mode: More details will be printed."
        shift # Remove --verbose from processing
        ;;
        *)
        # Unknown option, ignore or handle as error
        ;;
    esac
done

# --- Function to simulate scanning a directory ---
# In a real scenario, this function would:
# 1. Take a directory path as input.
# 2. Use `find` or `ls -R` to discover files and subdirectories.
# 3. Filter based on your criteria (e.g., ignore .git, node_modules).
# 4. Extract relevant information (appName, startUp, etc.).
# 5. Return structured data or print it if in dry-run mode.
scan_directory() {
    local base_dir="$1"
    local app_name="$2"
    local startup_link_base="$3"
    local secondary_link_example="$4"
    local comment_example="$5"

    if [ "$VERBOSE_OUTPUT" = true ]; then
        echo "Scanning: $base_dir for app '$app_name'..."
    fi

    # Simulate finding files/directories
    # In a real script, you'd use `find` or `ls` here
    local found_files=(
        "$base_dir/index.html"
        "$base_dir/css/style.css"
        "$base_dir/js/script.js"
        "$base_dir/config.json"
    )
    local found_dirs=(
        "$base_dir/assets"
        "$base_dir/images"
    )

    if [ "$DRY_RUN" = true ]; then
        echo "  App Name: $app_name"
        echo "  Directory: $base_dir"
        echo "  Startup Link (simulated): $startup_link_base"
        echo "  Secondary Link (simulated): $secondary_link_example"
        echo "  Comment (simulated): $comment_example"
        echo "  Discovered Files:"
        for file in "${found_files[@]}"; do
            echo "    - $file"
        done
        echo "  Discovered Directories:"
        for dir in "${found_dirs[@]}"; do
            echo "    - $dir"
        done
        echo ""
    fi

    # In a real script, you would construct a JSON object for this app
    # and add it to an array that will eventually be written to JSON_FILE.
    # For this example, we're just returning a placeholder if not dry run.
    if [ "$DRY_RUN" = false ]; then
        echo "{ \"appName\": \"$app_name\", \"directory\": \"$base_dir\", \"startUp\": \"$startup_link_base\", \"secondaryLink\": \"$secondary_link_example\", \"comment\": \"$comment_example\", \"status\": \"new\", \"lastUpdated\": \"$(date +"%Y-%m-%d")\" }"
    fi
}

# --- Main script execution ---
echo "Starting workOn.sh script..."

# Array to hold JSON objects for each app
APP_DATA=()

# --- Simulate scanning multiple project directories ---
# Replace this section with your actual directory scanning logic.
# For example, you might loop through subdirectories in a parent directory.

if [ "$VERBOSE_OUTPUT" = true ]; then
    echo "Simulating project discovery..."
fi

# Example 1: Project Alpha
APP_DATA+=("$(scan_directory "/var/www/html/project-alpha" "Project Alpha" "https://yourdomain.com/project-alpha/" "https://github.com/youruser/project-alpha" "Initial development phase.")")

# Example 2: Feature Beta
APP_DATA+=("$(scan_directory "/var/www/html/feature-beta" "Feature Beta" "https://yourdomain.com/feature-beta/index.html" "" "Under active development, testing required.")")

# Example 3: Utility Gamma
APP_DATA+=("$(scan_directory "/var/www/html/utility-gamma" "Utility Gamma" "https://yourdomain.com/utility-gamma/app.php" "https://www.bing.com" "Stable version, minor updates.")")

# --- End of project discovery simulation ---

if [ "$DRY_RUN" = false ]; then
    echo "Proceeding to update $JSON_FILE..."
    # --- Backup existing JSON file ---
    if [ -f "$JSON_FILE" ]; then
        TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
        BACKUP_FILE="${JSON_FILE}.bak.${TIMESTAMP}"
        cp "$JSON_FILE" "$BACKUP_FILE"
        if [ $? -eq 0 ]; then
            echo "Backed up $JSON_FILE to $BACKUP_FILE"
        else
            echo "Error: Failed to create backup of $JSON_FILE to $BACKUP_FILE." >&2
            exit 1 # Exit if backup fails
        fi
    else
        echo "$JSON_FILE does not exist. Creating new file."
    fi

    # --- Write new JSON data to file ---
    echo "[" > "$JSON_FILE"
    for i in "${!APP_DATA[@]}"; do
        echo "  ${APP_DATA[$i]}" >> "$JSON_FILE"
        if [ "$i" -lt $(( ${#APP_DATA[@]} - 1 )) ]; then
            echo "," >> "$JSON_FILE"
        fi
    done
    echo "]" >> "$JSON_FILE"

    if [ $? -eq 0 ]; then
        echo "Finished updating $JSON_FILE successfully."
    else
        echo "Error: Failed to write to $JSON_FILE. Check permissions." >&2
        exit 1 # Exit if write fails
    fi
else
    echo "Dry run complete. No changes made to $JSON_FILE."
fi

echo "Script finished."

