#!/bin/bash

# End-to-End Testing Script for Zero-Knowledge Gaming Cloud
# Tests: Worker → Gateway → NATS → Storage → Dashboard

set -e

echo "🚀 Starting Zero-Knowledge Cloud E2E Test..."

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

# Step 1: Check if Docker Compose is running
echo -e "\n${YELLOW}[1/7]${NC} Checking Docker Compose status..."
if ! docker-compose ps | grep -q "Up"; then
    echo -e "${RED}❌ Docker Compose is not running. Start with: docker-compose up${NC}"
    exit 1
fi
echo -e "${GREEN}✅ Docker Compose is running${NC}"

# Step 2: Verify NATS connectivity
echo -e "\n${YELLOW}[2/7]${NC} Testing NATS connectivity..."
if docker-compose exec -T nats nats-server --version > /dev/null 2>&1; then
    echo -e "${GREEN}✅ NATS is healthy${NC}"
else
    echo -e "${RED}❌ NATS is not responding${NC}"
    exit 1
fi

# Step 3: Check PostgreSQL
echo -e "\n${YELLOW}[3/7]${NC} Testing PostgreSQL connectivity..."
if docker-compose exec -T postgres-master pg_isready -U admin > /dev/null 2>&1; then
    echo -e "${GREEN}✅ PostgreSQL is healthy${NC}"
else
    echo -e "${RED}❌ PostgreSQL is not responding${NC}"
    exit 1
fi

# Step 4: Verify Gateway HTTP endpoint
echo -e "\n${YELLOW}[4/7]${NC} Testing Gateway API..."
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/api/auth/login/begin -X POST -H "Content-Type: application/json" -d '{"username":"test"}')
if [ "$HTTP_CODE" -eq 200 ] || [ "$HTTP_CODE" -eq 400 ]; then
    echo -e "${GREEN}✅ Gateway API is responding (HTTP $HTTP_CODE)${NC}"
else
    echo -e "${RED}❌ Gateway API failed (HTTP $HTTP_CODE)${NC}"
    exit 1
fi

# Step 5: Check Worker logs for encryption
echo -e "\n${YELLOW}[5/7]${NC} Verifying Worker encryption..."
WORKER_LOGS=$(docker-compose logs worker --tail=20 2>&1)
if echo "$WORKER_LOGS" | grep -q "Sent MOCK event"; then
    echo -e "${GREEN}✅ Worker is sending encrypted events${NC}"
else
    echo -e "${RED}❌ Worker is not sending events${NC}"
    echo "Latest worker logs:"
    echo "$WORKER_LOGS"
fi

# Step 6: Check Storage Node persistence
echo -e "\n${YELLOW}[6/7]${NC} Verifying Storage Node persistence..."
STORAGE_LOGS=$(docker-compose logs storage-node --tail=20 2>&1)
if echo "$STORAGE_LOGS" | grep -q "Persisted"; then
    echo -e "${GREEN}✅ Storage Node is persisting logs${NC}"
else
    echo -e "${YELLOW}⚠️  Storage Node might not be persisting (check manually)${NC}"
fi

# Step 7: Verify Dashboard accessibility
echo -e "\n${YELLOW}[7/7]${NC} Testing Dashboard frontend..."
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:5173)
if [ "$HTTP_CODE" -eq 200 ]; then
    echo -e "${GREEN}✅ Dashboard is accessible (HTTP $HTTP_CODE)${NC}"
else
    echo -e "${RED}❌ Dashboard is not responding (HTTP $HTTP_CODE)${NC}"
    exit 1
fi

# Final Summary
echo -e "\n${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${GREEN}✅ All E2E tests passed!${NC}"
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
echo "📊 Next steps:"
echo "  1. Open http://localhost:5173 in your browser"
echo "  2. Login with username 'demo' (WebAuthn will prompt)"
echo "  3. Watch real-time encrypted logs flowing through the system"
echo ""
echo "🔍 Inspect components:"
echo "  docker-compose logs gateway"
echo "  docker-compose logs storage-node"
echo "  docker-compose logs worker"
echo "  docker-compose logs discord-bot"
