#!/bin/bash
set -e

MIDDLEWARE_URL="http://localhost:8080"
TEST_TOPIC="test-topic"

echo "🧪 Testing Slack to ntfy middleware..."

# Check if service is running
echo "Checking if middleware is running..."
if ! curl -s "$MIDDLEWARE_URL/health" > /dev/null; then
    echo "❌ Middleware is not running on $MIDDLEWARE_URL"
    echo "Please start it with: docker compose up -d"
    exit 1
fi

echo "✅ Middleware is running"

# Test health endpoint
echo "Testing health endpoint..."
HEALTH_RESPONSE=$(curl -s "$MIDDLEWARE_URL/health")
echo "Health response: $HEALTH_RESPONSE"

# Test Slack webhook format
echo "Testing Slack webhook format..."
curl -X POST "$MIDDLEWARE_URL/$TEST_TOPIC" \
     -H 'Content-Type: application/json' \
     -d '{"text": "🧪 Test alert from Slack to ntfy middleware test script"}' \
     -w "\nHTTP Status: %{http_code}\n"

# Test plain text format
echo "Testing plain text format..."
curl -X POST "$MIDDLEWARE_URL/$TEST_TOPIC" \
     -H 'Content-Type: text/plain' \
     -d 'Plain text test alert' \
     -w "\nHTTP Status: %{http_code}\n"

echo "✅ Tests completed!"
echo "Check your ntfy client to see if messages were delivered to topic: $TEST_TOPIC"
