#!/bin/bash
# Test Matomo Analytics Integration
echo "🔍 Testing Matomo Analytics Integration..."
echo "================================================"

# Test 1: Check if CSP includes Matomo hash
echo "1. Checking Content Security Policy..."
CSP_RESPONSE=$(curl -s -I https://colinknapp.com | grep -i "content-security-policy")
if echo "$CSP_RESPONSE" | grep -q "sha256-aSi4/F2xxTg7cs3QbVq7ncUMa1ivQeVC8umnPRDtFyM="; then
    echo "✅ CSP includes Matomo script hash"
else
    echo "❌ CSP missing Matomo script hash"
fi

if echo "$CSP_RESPONSE" | grep -q "https://metrics.nixc.us"; then
    echo "✅ CSP allows metrics.nixc.us domain"
else
    echo "❌ CSP missing metrics.nixc.us domain"
fi

# Test 2: Check if Matomo script is in header include
echo ""
echo "2. Checking Matomo script in header include..."
HEADER_RESPONSE=$(curl -s https://colinknapp.com/includes/header.html)
if echo "$HEADER_RESPONSE" | grep -q "_paq"; then
    echo "✅ Matomo script found in header include"
else
    echo "❌ Matomo script missing from header include"
fi

# Test 3: Test Matomo tracking pixel
echo ""
echo "3. Testing Matomo tracking pixel..."
PIXEL_RESPONSE=$(curl -s -w "%{http_code}" "https://metrics.nixc.us/matomo.php?idsite=3&rec=1&action_name=test&url=https://colinknapp.com" -o /dev/null)
if [ "$PIXEL_RESPONSE" = "200" ]; then
    echo "✅ Matomo tracking pixel responds (HTTP 200)"
else
    echo "❌ Matomo tracking pixel failed (HTTP $PIXEL_RESPONSE)"
fi

# Test 4: Check if matomo.js is accessible
echo ""
echo "4. Testing Matomo JavaScript file..."
JS_RESPONSE=$(curl -s -w "%{http_code}" "https://metrics.nixc.us/matomo.js" -o /dev/null)
if [ "$JS_RESPONSE" = "200" ]; then
    echo "✅ Matomo JavaScript file accessible (HTTP 200)"
else
    echo "❌ Matomo JavaScript file failed (HTTP $JS_RESPONSE)"
fi

echo ""
echo "================================================"
echo "🎯 Test complete! If all tests pass, Matomo should be working."
echo "💡 Visit https://colinknapp.com and check browser console for any CSP errors."
echo "📊 Check your Matomo dashboard at https://metrics.nixc.us for real-time data." 