#!/bin/bash
# Test Grafana Cloud Loki integration locally
# Usage: ./scripts/test-grafana-cloud.sh

set -e

echo "🧪 ThrillWiki Grafana Cloud Loki Integration Test"
echo "=================================================="
echo ""

# Check required environment variables
if [ -z "$GRAFANA_LOKI_URL" ]; then
  echo "❌ GRAFANA_LOKI_URL environment variable is not set"
  echo ""
  echo "Please set the following environment variables:"
  echo "  export GRAFANA_LOKI_URL=\"https://logs-prod-us-central1.grafana.net\""
  echo "  export GRAFANA_LOKI_USERNAME=\"123456\""
  echo "  export GRAFANA_LOKI_PASSWORD=\"glc_xxxxxxxxxxxxx\""
  exit 1
fi

if [ -z "$GRAFANA_LOKI_USERNAME" ]; then
  echo "❌ GRAFANA_LOKI_USERNAME environment variable is not set"
  exit 1
fi

if [ -z "$GRAFANA_LOKI_PASSWORD" ]; then
  echo "❌ GRAFANA_LOKI_PASSWORD environment variable is not set"
  exit 1
fi

echo "✅ Environment variables configured"
echo "   Loki URL: $GRAFANA_LOKI_URL"
echo "   Username: $GRAFANA_LOKI_USERNAME"
echo ""

# Test connection by sending a test log
echo "🔍 Testing Grafana Cloud Loki connection..."
timestamp=$(date +%s)000000000

response=$(curl -s -w "\n%{http_code}" \
  --max-time 10 \
  -u "$GRAFANA_LOKI_USERNAME:$GRAFANA_LOKI_PASSWORD" \
  -H "Content-Type: application/json" \
  -H "User-Agent: ThrillWiki-Test-Script/1.0" \
  -X POST "$GRAFANA_LOKI_URL/loki/api/v1/push" \
  -d "{
    \"streams\": [{
      \"stream\": {
        \"job\": \"test_script\",
        \"source\": \"local\",
        \"test_type\": \"connection_test\"
      },
      \"values\": [[\"$timestamp\", \"Test log from local machine at $(date)\"]]
    }]
  }")

http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | head -n -1)

if [ "$http_code" = "204" ] || [ "$http_code" = "200" ]; then
  echo "✅ Successfully sent test log to Grafana Cloud Loki!"
  echo ""
  echo "📊 View your logs in Grafana Cloud:"
  echo "   1. Go to your Grafana Cloud instance"
  echo "   2. Navigate to Explore"
  echo "   3. Select your Loki data source"
  echo "   4. Run query: {job=\"test_script\"}"
  echo ""
else
  echo "❌ Failed to connect to Grafana Cloud Loki"
  echo "   HTTP Status: $http_code"
  if [ -n "$body" ]; then
    echo "   Response: $body"
  fi
  echo ""
  echo "Common issues:"
  echo "  - Invalid API key (check GRAFANA_LOKI_PASSWORD)"
  echo "  - Wrong instance ID (check GRAFANA_LOKI_USERNAME)"
  echo "  - Incorrect region in URL (check GRAFANA_LOKI_URL)"
  exit 1
fi

# Run a sample Playwright test with Loki reporter
echo "🧪 Running sample Playwright test with Loki reporter..."
echo ""

if [ -d "tests/e2e/auth" ]; then
  npx playwright test tests/e2e/auth/login.spec.ts --project=chromium --max-failures=1 || true
  echo ""
  echo "✅ Test completed (check above for test results)"
  echo ""
  echo "📊 View test logs in Grafana Cloud:"
  echo "   Query: {job=\"playwright_tests\"}"
  echo "   Filter by browser: {job=\"playwright_tests\", browser=\"chromium\"}"
  echo "   Filter by status: {job=\"playwright_tests\", status=\"passed\"}"
else
  echo "⚠️  No tests found in tests/e2e/auth/"
  echo "   Skipping Playwright test execution"
fi

echo ""
echo "🎉 Grafana Cloud Loki integration test complete!"
