#!/bin/bash

# NPC Integration Test Script
# Tests that the NPC system is properly integrated between server and client

echo "🧪 Testing NPC Integration..."
echo ""

# Check if server NPC imports are uncommented
echo "1️⃣ Checking server NPC imports..."
if grep -q "^import { initNpcManager } from" server/ws/ws.ts; then
    echo "✅ NPC Manager import is active"
else
    echo "❌ NPC Manager import is commented out"
    exit 1
fi

# Check if NPC Manager initialization is uncommented
echo ""
echo "2️⃣ Checking NPC Manager initialization..."
if grep -q "^function broadcastToClients" server/ws/ws.ts; then
    echo "✅ broadcastToClients function is active"
else
    echo "❌ broadcastToClients function is missing"
    exit 1
fi

# Check if FrontendNPCManager exists
echo ""
echo "3️⃣ Checking FrontendNPCManager..."
if [ -f "src/game/systems/npc/FrontendNPCManager.ts" ]; then
    echo "✅ FrontendNPCManager exists"
else
    echo "❌ FrontendNPCManager not found"
    exit 1
fi

# Check if NPCRenderer exists
echo ""
echo "4️⃣ Checking NPCRenderer..."
if [ -f "src/game/systems/npc/NPCRenderer.ts" ]; then
    echo "✅ NPCRenderer exists"
else
    echo "❌ NPCRenderer not found"
    exit 1
fi

# Check if WebSocketManager handles NPC data
echo ""
echo "5️⃣ Checking WebSocketManager NPC handling..."
if grep -q "isNPCData" src/game/systems/WebSocketManager.ts; then
    echo "✅ WebSocketManager has NPC data detection"
else
    echo "❌ WebSocketManager missing NPC data detection"
    exit 1
fi

# Check if MainScene integrates FrontendNPCManager
echo ""
echo "6️⃣ Checking MainScene integration..."
if grep -q "FrontendNPCManager" src/game/scenes/MainScene.ts; then
    echo "✅ MainScene integrates FrontendNPCManager"
else
    echo "❌ MainScene missing FrontendNPCManager integration"
    exit 1
fi

# Check if server NPC modules exist
echo ""
echo "7️⃣ Checking server NPC modules..."
if [ -f "server/ws/npc/npcManager.ts" ] && [ -f "server/ws/npc/graphData.ts" ]; then
    echo "✅ Server NPC modules exist"
else
    echo "❌ Server NPC modules missing"
    exit 1
fi

# Check integration guide
echo ""
echo "8️⃣ Checking integration documentation..."
if [ -f "NPC_INTEGRATION_GUIDE.md" ]; then
    echo "✅ Integration guide exists"
else
    echo "⚠️  Integration guide not found (optional)"
fi

echo ""
echo "================================================"
echo "✅ ALL CHECKS PASSED!"
echo "================================================"
echo ""
echo "📝 Next Steps:"
echo "1. Start the server: cd server/ws && deno run --allow-all ws.ts"
echo "2. Look for: [NPC Manager] ✅ NPC system started successfully"
echo "3. Start the client: npm run dev"
echo "4. Join the game and check browser console for: [WebSocketManager] Detected NPC data"
echo "5. You should see blue-tinted NPCs moving around the map"
echo ""
echo "🔧 Configuration:"
echo "   Set environment variables to customize NPC behavior:"
echo "   - NPC_COUNT=3 (number of NPCs)"
echo "   - NPC_PROXIMITY_RADIUS=50 (pause distance)"
echo "   - NPC_SPEED=120 (movement speed)"
echo "   - NPC_DEBUG=true (enable debug logs)"
echo ""
echo "📖 See NPC_INTEGRATION_GUIDE.md for detailed documentation"
echo ""
