#!/bin/bash
# Script de diagnostic pour le frontend Veza

echo "=== DIAGNOSTIC FRONTEND VEZA ==="
echo ""

# Test 1: Homepage
echo "1. Test Homepage:"
curl -k -s -I https://10.10.10.6/ | head -3
echo ""

# Test 2: API
echo "2. Test API:"
curl -k -s https://10.10.10.6/api/v1/health
echo ""
echo ""

# Test 3: JS Bundle
echo "3. Test JS Bundle:"
JS_FILE=$(curl -k -s https://10.10.10.6/ | grep -o 'src="[^"]*\.js"' | sed 's/src="//;s/"//' | head -1)
echo "JS file: $JS_FILE"
curl -k -s -I "https://10.10.10.6$JS_FILE" | head -3
echo ""

# Test 4: Chunks
echo "4. Test Chunks:"
curl -k -s "https://10.10.10.6$JS_FILE" | grep -o 'from"[^"]*"' | head -5 | while read chunk; do
    CHUNK_PATH=$(echo "$chunk" | sed 's/from"\.\///;s/"//')
    echo "Testing: $CHUNK_PATH"
    curl -k -s -I "https://10.10.10.6/$CHUNK_PATH" 2>&1 | head -1
done
echo ""

# Test 5: Service Worker
echo "5. Test Service Worker:"
curl -k -s -I https://10.10.10.6/sw.js | head -3
echo ""

# Test 6: CSS
echo "6. Test CSS:"
CSS_FILE=$(curl -k -s https://10.10.10.6/ | grep -o 'href="[^"]*\.css"' | sed 's/href="//;s/"//' | head -1)
echo "CSS file: $CSS_FILE"
curl -k -s -I "https://10.10.10.6$CSS_FILE" | head -3
echo ""

# Test 7: CORS
echo "7. Test CORS:"
curl -k -s -X OPTIONS https://10.10.10.6/api/v1/health \
  -H "Origin: https://10.10.10.6" \
  -H "Access-Control-Request-Method: GET" \
  -I 2>&1 | grep -E "access-control" | head -3
echo ""

echo "=== FIN DU DIAGNOSTIC ==="
echo ""
echo "Si tous les tests passent mais l'écran est noir:"
echo "1. Ouvrez la console du navigateur (F12)"
echo "2. Vérifiez les erreurs JavaScript"
echo "3. Vérifiez l'onglet Network pour les requêtes échouées"
echo "4. Désactivez le Service Worker: Application > Service Workers > Unregister"
