#!/bin/bash

ORIG_FILE="lib/controller/home/map_passenger_controller.dart"
ALL_FILES="lib/controller/home/map/location_search_controller.dart lib/controller/home/map/map_engine_controller.dart lib/controller/home/map/map_screen_binding.dart lib/controller/home/map/map_socket_controller.dart lib/controller/home/map/nearby_drivers_controller.dart lib/controller/home/map/ride_lifecycle_controller.dart lib/controller/home/map/ui_interactions_controller.dart"

echo "Extracting methods from original controller..."
# Methods typically start with spaces and have patterns like:
# returnType methodName( or methodName(
# Let's extract words that precede ( on lines that don't start with keywords (if, for, while, switch, catch, etc.)
# We will use awk to parse.
METHODS=$(cat "$ORIG_FILE" | awk '
# Skip single-line comments
/\/\// { next }
# Skip imports and class declarations
/import/ || /class/ { next }
# Find lines with "("
/\(/ {
    # Replace anything inside parentheses and curly braces to simplify
    gsub(/\(.*\)/, "()")
    # Find word before "()"
    for (i = 1; i <= NF; i++) {
        if ($i ~ /[a-zA-Z0-9_]+\(\)/) {
            name = $i
            sub(/\(\)/, "", name)
            # Remove any leading modifiers like async, Future, static, etc.
            # Only keep valid identifiers that are not control keywords
            if (name !~ /^(if|for|while|switch|catch|super|await|print|assert|dynamic|void|return|with|override|get|set|else|try|final|const|var|late|static|factory|new|abstract|covariant|external|operator|part|required|typedef|yield)$/ && name ~ /^[a-zA-Z_][a-zA-Z0-9_]*$/) {
                print name
            }
        }
    }
}' | sort -u)

echo "Extracting fields/variables from original controller..."
# Fields are usually declared inside the class at the beginning of lines or indented.
# e.g., RxBool isSearching = false.obs; or String? rideId;
VARS=$(cat "$ORIG_FILE" | awk '
/\/\// { next }
/import/ || /class/ { next }
# Lines ending with ";" or containing "=" followed by ";"
/;/ {
    # Extract words that look like declarations.
    # We look for typical type names or var/final followed by variable name
    for (i = 1; i < NF; i++) {
        if ($i ~ /^(var|final|const|late|RxBool|RxInt|RxDouble|RxString|RxList|RxMap|RxSet|Rx|String|int|double|bool|List|Map|Set|Timer|LatLng|Position|SiroMapController)$/) {
            # The next field might be the variable name, or it might have a type like String?
            name = $(i+1)
            # Remove trailing ?, ;, =
            sub(/\?/, "", name)
            sub(/;/, "", name)
            sub(/=/, "", name)
            if (name ~ /^[a-zA-Z_][a-zA-Z0-9_]*$/) {
                print name
            }
        }
    }
}' | sort -u)

echo "Checking split files for methods..."
echo "--- MISSING METHODS ---"
MISSING_METHODS_COUNT=0
# Create a temporary file with all split contents to search efficiently
cat $ALL_FILES > lib/controller/home/temp_split_combined.txt

for method in $METHODS; do
    # Search for this method name as a whole word in split controllers
    FOUND=$(grep -w "$method" lib/controller/home/temp_split_combined.txt 2>/dev/null)
    if [ -z "$FOUND" ]; then
        echo " - $method"
        MISSING_METHODS_COUNT=$((MISSING_METHODS_COUNT+1))
    fi
done
echo "Total missing methods: $MISSING_METHODS_COUNT"

echo ""
echo "Checking split files for variables/fields..."
echo "--- MISSING VARIABLES ---"
MISSING_VARS_COUNT=0
for var in $VARS; do
    FOUND=$(grep -w "$var" lib/controller/home/temp_split_combined.txt 2>/dev/null)
    if [ -z "$FOUND" ]; then
        echo " - $var"
        MISSING_VARS_COUNT=$((MISSING_VARS_COUNT+1))
    fi
done
echo "Total missing variables: $MISSING_VARS_COUNT"

# Clean up temp file
rm lib/controller/home/temp_split_combined.txt
