#!/bin/bash

# Container Sync Check Script
# Compares files between running containers and local filesystem

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

echo "🔍 Container Sync Check"
echo "======================="
echo ""

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

DIFFERENCES_FOUND=0

# Function to check file differences
check_file() {
    local container=$1
    local container_path=$2
    local local_path=$3
    
    # Check if local file exists
    if [ ! -f "$local_path" ]; then
        echo -e "${YELLOW}⚠️  Local file missing: $local_path${NC}"
        DIFFERENCES_FOUND=1
        return
    fi
    
    # Get file from container to temp location
    local temp_file=$(mktemp)
    if docker exec "$container" test -f "$container_path" 2>/dev/null; then
        docker exec "$container" cat "$container_path" > "$temp_file" 2>/dev/null || {
            echo -e "${YELLOW}⚠️  Cannot read from container: $container:$container_path${NC}"
            rm -f "$temp_file"
            DIFFERENCES_FOUND=1
            return
        }
    else
        echo -e "${YELLOW}⚠️  File not in container: $container:$container_path${NC}"
        rm -f "$temp_file"
        DIFFERENCES_FOUND=1
        return
    fi
    
    # Compare files
    if ! diff -q "$temp_file" "$local_path" > /dev/null 2>&1; then
        local container_lines=$(wc -l < "$temp_file")
        local local_lines=$(wc -l < "$local_path")
        echo -e "${RED}❌ DIFFERENT: $local_path${NC}"
        echo "   Container: $container_lines lines"
        echo "   Local:     $local_lines lines"
        echo "   To sync: docker cp $container:$container_path $local_path"
        DIFFERENCES_FOUND=1
    else
        echo -e "${GREEN}✅ OK: $local_path${NC}"
    fi
    
    rm -f "$temp_file"
}

# Function to check directory recursively
check_directory() {
    local container=$1
    local container_dir=$2
    local local_dir=$3
    local pattern=$4
    
    echo ""
    echo "Checking directory: $local_dir"
    echo "---"
    
    # Get list of files from container
    local files=$(docker exec "$container" find "$container_dir" -type f -name "$pattern" 2>/dev/null || echo "")
    
    if [ -z "$files" ]; then
        echo -e "${YELLOW}⚠️  No files found in container: $container:$container_dir${NC}"
        return
    fi
    
    while IFS= read -r container_file; do
        # Convert container path to local path
        local relative_path="${container_file#$container_dir/}"
        local local_file="$local_dir/$relative_path"
        
        check_file "$container" "$container_file" "$local_file"
    done <<< "$files"
}

echo "📦 Checking echoes_of_the_ashes_map container..."
echo "================================================"

# Check web-map files
check_file "echoes_of_the_ashes_map" "/app/web-map/server.py" "web-map/server.py"
check_file "echoes_of_the_ashes_map" "/app/web-map/editor_enhanced.js" "web-map/editor_enhanced.js"
check_file "echoes_of_the_ashes_map" "/app/web-map/editor.html" "web-map/editor.html"
check_file "echoes_of_the_ashes_map" "/app/web-map/index.html" "web-map/index.html"
check_file "echoes_of_the_ashes_map" "/app/web-map/map.js" "web-map/map.js"

echo ""
echo "📦 Checking echoes_of_the_ashes_api container..."
echo "================================================"

# Check API files
check_file "echoes_of_the_ashes_api" "/app/api/main.py" "api/main.py"
check_file "echoes_of_the_ashes_api" "/app/api/database.py" "api/database.py"
check_file "echoes_of_the_ashes_api" "/app/api/game_logic.py" "api/game_logic.py"
check_file "echoes_of_the_ashes_api" "/app/api/background_tasks.py" "api/background_tasks.py"

# Check API routers
if docker exec echoes_of_the_ashes_api test -d "/app/api/routers" 2>/dev/null; then
    check_directory "echoes_of_the_ashes_api" "/app/api/routers" "api/routers" "*.py"
fi

# Check API services
if docker exec echoes_of_the_ashes_api test -d "/app/api/services" 2>/dev/null; then
    check_directory "echoes_of_the_ashes_api" "/app/api/services" "api/services" "*.py"
fi

echo ""
echo "📦 Checking echoes_of_the_ashes_pwa container..."
echo "================================================"

# Check PWA source files
check_file "echoes_of_the_ashes_pwa" "/app/src/App.tsx" "pwa/src/App.tsx"
check_file "echoes_of_the_ashes_pwa" "/app/src/main.tsx" "pwa/src/main.tsx"

# Check PWA components
if docker exec echoes_of_the_ashes_pwa test -d "/app/src/components" 2>/dev/null; then
    check_directory "echoes_of_the_ashes_pwa" "/app/src/components" "pwa/src/components" "*.tsx"
fi

# Check PWA game components
if docker exec echoes_of_the_ashes_pwa test -d "/app/src/components/game" 2>/dev/null; then
    check_directory "echoes_of_the_ashes_pwa" "/app/src/components/game" "pwa/src/components/game" "*.tsx"
fi

echo ""
echo "📊 Summary"
echo "=========="

if [ $DIFFERENCES_FOUND -eq 0 ]; then
    echo -e "${GREEN}✅ All checked files are in sync!${NC}"
    exit 0
else
    echo -e "${RED}❌ Differences found! Review the output above.${NC}"
    echo ""
    echo "To sync all files from containers, run:"
    echo "  ./sync_from_containers.sh"
    exit 1
fi
