#!/bin/bash

# Exit on any error
set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Set the DSN
DSN="https://725ff4678a4b447c8aa22450b64d66ae@bugsink.aenow.com/6"
echo -e "${BLUE}Testing with multiple environment variable options for DSN${NC}"
echo "DSN: $DSN"

# Function to verify results
verify_result() {
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✅ Test passed${NC}"
    else
        echo -e "${RED}❌ Test failed${NC}"
        exit 1
    fi
}

# Build the application using build.sh
echo -e "\n${BLUE}Building go-glitch using build.sh...${NC}"
./build.sh
verify_result

# Check if the darwin_arm64 binary exists
if [ ! -f "./dist/go-glitch_darwin_arm64" ]; then
  echo -e "${RED}❌ Binary for darwin/arm64 not found. Make sure build.sh completed successfully.${NC}"
  exit 1
else
  echo -e "${GREEN}✅ Binary found${NC}"
fi

# Check for running server instances and kill them
echo -e "\n${BLUE}Checking for existing go-glitch server instances...${NC}"
pkill -f "go-glitch.*server" || true
sleep 1

# Test 1a: Using GLITCHTIP_DSN with plain text
echo -e "\n${YELLOW}Test 1a: Using GLITCHTIP_DSN environment variable with plain text...${NC}"
export GLITCHTIP_DSN="$DSN"
export SENTRY_DSN=""
export BUGSINK_DSN=""
echo "Plain text test with GLITCHTIP_DSN" | ./dist/go-glitch_darwin_arm64
verify_result

# Test 1b: Using GLITCHTIP_DSN with JSON
echo -e "\n${YELLOW}Test 1b: Using GLITCHTIP_DSN environment variable with JSON...${NC}"
echo '{"log_level": "error", "message": "JSON test with GLITCHTIP_DSN", "service": "test-suite", "test_id": "1b"}' | ./dist/go-glitch_darwin_arm64
verify_result

# Test 2a: Using SENTRY_DSN with plain text
echo -e "\n${YELLOW}Test 2a: Using SENTRY_DSN environment variable with plain text...${NC}"
export GLITCHTIP_DSN=""
export SENTRY_DSN="$DSN"
export BUGSINK_DSN=""
echo "Plain text test with SENTRY_DSN" | ./dist/go-glitch_darwin_arm64
verify_result

# Test 2b: Using SENTRY_DSN with JSON
echo -e "\n${YELLOW}Test 2b: Using SENTRY_DSN environment variable with JSON...${NC}"
echo '{"log_level": "error", "message": "JSON test with SENTRY_DSN", "service": "test-suite", "test_id": "2b"}' | ./dist/go-glitch_darwin_arm64
verify_result

# Test 3a: Using BUGSINK_DSN with plain text
echo -e "\n${YELLOW}Test 3a: Using BUGSINK_DSN environment variable with plain text...${NC}"
export GLITCHTIP_DSN=""
export SENTRY_DSN=""
export BUGSINK_DSN="$DSN"
echo "Plain text test with BUGSINK_DSN" | ./dist/go-glitch_darwin_arm64
verify_result

# Test 3b: Using BUGSINK_DSN with JSON
echo -e "\n${YELLOW}Test 3b: Using BUGSINK_DSN environment variable with JSON...${NC}"
echo '{"log_level": "error", "message": "JSON test with BUGSINK_DSN", "service": "test-suite", "test_id": "3b"}' | ./dist/go-glitch_darwin_arm64
verify_result

# Test environment variable priority (GLITCHTIP_DSN should take precedence)
echo -e "\n${YELLOW}Test 4: Testing environment variable priority...${NC}"
export GLITCHTIP_DSN="$DSN"
export SENTRY_DSN="$DSN"
export BUGSINK_DSN="$DSN"
echo "Testing environment variable priority" | ./dist/go-glitch_darwin_arm64
verify_result

# Start the server with GLITCHTIP_DSN for webhook tests
echo -e "\n${BLUE}Starting go-glitch server with GLITCHTIP_DSN...${NC}"
export GLITCHTIP_DSN="$DSN"
export SENTRY_DSN=""
export BUGSINK_DSN=""
./dist/go-glitch_darwin_arm64 server &
SERVER_PID=$!

# Give the server time to start
echo "Waiting for server to start..."
sleep 2

# Test 5: Send webhook request
echo -e "\n${YELLOW}Test 5: Sending a webhook request...${NC}"
curl -X POST -H "Content-Type: application/json" -d '{"log_level": "error", "message": "Error via webhook", "service": "test-suite", "test_id": "5"}' http://localhost:5050/webhook
verify_result

# Test 6: Send webhook batch request
echo -e "\n${YELLOW}Test 6: Sending a webhook batch request...${NC}"
curl -X POST -H "Content-Type: application/json" -d '[{"log_level": "error", "message": "Error in batch", "service": "test-suite", "test_id": "6.1"},{"log_level": "warning", "message": "Warning in batch", "service": "test-suite", "test_id": "6.2"}]' http://localhost:5050/webhook
verify_result

# Clean up the server
echo -e "\n${BLUE}Tests completed. Shutting down server...${NC}"
kill $SERVER_PID || true

echo -e "\n${GREEN}All tests passed successfully!${NC}" 