#!/bin/bash

# Sync Files FROM Containers TO Local Filesystem
# Recovers the latest versions from running containers

set -e

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

echo "🔄 Syncing files FROM containers TO local filesystem"
echo "====================================================="
echo ""

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# Function to sync a single file
sync_file() {
    local container=$1
    local container_path=$2
    local local_path=$3
    
    if docker exec "$container" test -f "$container_path" 2>/dev/null; then
        echo -e "${GREEN}📥 Copying: $local_path${NC}"
        docker cp "$container:$container_path" "$local_path"
    else
        echo -e "${YELLOW}⚠️  Skipping (not in container): $local_path${NC}"
    fi
}

# Function to sync directory
sync_directory() {
    local container=$1
    local container_dir=$2
    local local_dir=$3
    
    echo ""
    echo "Syncing directory: $local_dir"
    echo "---"
    
    # Create local directory if it doesn't exist
    mkdir -p "$local_dir"
    
    # Copy entire directory
    if docker exec "$container" test -d "$container_dir" 2>/dev/null; then
        echo -e "${GREEN}📥 Copying directory: $container_dir -> $local_dir${NC}"
        docker cp "$container:$container_dir/." "$local_dir/"
    else
        echo -e "${YELLOW}⚠️  Directory not in container: $container_dir${NC}"
    fi
}

echo "📦 Syncing from echoes_of_the_ashes_map..."
echo "==========================================="

sync_file "echoes_of_the_ashes_map" "/app/web-map/server.py" "web-map/server.py"
sync_file "echoes_of_the_ashes_map" "/app/web-map/editor_enhanced.js" "web-map/editor_enhanced.js"
sync_file "echoes_of_the_ashes_map" "/app/web-map/editor.html" "web-map/editor.html"
sync_file "echoes_of_the_ashes_map" "/app/web-map/index.html" "web-map/index.html"
sync_file "echoes_of_the_ashes_map" "/app/web-map/map.js" "web-map/map.js"
sync_file "echoes_of_the_ashes_map" "/app/web-map/README.md" "web-map/README.md"

echo ""
echo "📦 Syncing from echoes_of_the_ashes_api..."
echo "==========================================="

sync_file "echoes_of_the_ashes_api" "/app/api/main.py" "api/main.py"
sync_file "echoes_of_the_ashes_api" "/app/api/database.py" "api/database.py"
sync_file "echoes_of_the_ashes_api" "/app/api/game_logic.py" "api/game_logic.py"
sync_file "echoes_of_the_ashes_api" "/app/api/background_tasks.py" "api/background_tasks.py"

# Sync routers directory
if docker exec echoes_of_the_ashes_api test -d "/app/api/routers" 2>/dev/null; then
    sync_directory "echoes_of_the_ashes_api" "/app/api/routers" "api/routers"
fi

# Sync services directory
if docker exec echoes_of_the_ashes_api test -d "/app/api/services" 2>/dev/null; then
    sync_directory "echoes_of_the_ashes_api" "/app/api/services" "api/services"
fi

# Sync core directory
if docker exec echoes_of_the_ashes_api test -d "/app/api/core" 2>/dev/null; then
    sync_directory "echoes_of_the_ashes_api" "/app/api/core" "api/core"
fi

echo ""
echo "📦 Syncing from echoes_of_the_ashes_pwa..."
echo "==========================================="

sync_file "echoes_of_the_ashes_pwa" "/app/src/App.tsx" "pwa/src/App.tsx"
sync_file "echoes_of_the_ashes_pwa" "/app/src/App.css" "pwa/src/App.css"
sync_file "echoes_of_the_ashes_pwa" "/app/src/main.tsx" "pwa/src/main.tsx"
sync_file "echoes_of_the_ashes_pwa" "/app/src/index.css" "pwa/src/index.css"

# Sync components
if docker exec echoes_of_the_ashes_pwa test -d "/app/src/components" 2>/dev/null; then
    sync_directory "echoes_of_the_ashes_pwa" "/app/src/components" "pwa/src/components"
fi

# Sync contexts
if docker exec echoes_of_the_ashes_pwa test -d "/app/src/contexts" 2>/dev/null; then
    sync_directory "echoes_of_the_ashes_pwa" "/app/src/contexts" "pwa/src/contexts"
fi

# Sync services
if docker exec echoes_of_the_ashes_pwa test -d "/app/src/services" 2>/dev/null; then
    sync_directory "echoes_of_the_ashes_pwa" "/app/src/services" "pwa/src/services"
fi

# Sync hooks
if docker exec echoes_of_the_ashes_pwa test -d "/app/src/hooks" 2>/dev/null; then
    sync_directory "echoes_of_the_ashes_pwa" "/app/src/hooks" "pwa/src/hooks"
fi

# Sync utils
if docker exec echoes_of_the_ashes_pwa test -d "/app/src/utils" 2>/dev/null; then
    sync_directory "echoes_of_the_ashes_pwa" "/app/src/utils" "pwa/src/utils"
fi

echo ""
echo -e "${GREEN}✅ Sync complete!${NC}"
echo ""
echo "Run './check_container_sync.sh' to verify all files are now in sync."
