#!/bin/bash

# Generate Test Traffic to Services
# This script generates API calls to verify telemetry data collection

set -e

NAMESPACE="bakery-ia"
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}  Generating Test Traffic for SigNoz Verification${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""

# Check if ingress is accessible
echo -e "${BLUE}Step 1: Verifying Gateway Access${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

GATEWAY_POD=$(kubectl get pods -n $NAMESPACE -l app=gateway --field-selector=status.phase=Running -o jsonpath='{.items[0].metadata.name}' 2>/dev/null)
if [[ -z "$GATEWAY_POD" ]]; then
    echo -e "${YELLOW}⚠ Gateway pod not running. Starting port-forward...${NC}"
    # Port forward in background
    kubectl port-forward -n $NAMESPACE svc/gateway-service 8000:8000 &
    PORT_FORWARD_PID=$!
    sleep 3
    API_URL="http://localhost:8000"
else
    echo -e "${GREEN}✓ Gateway is running: $GATEWAY_POD${NC}"
    # Use internal service
    API_URL="http://gateway-service.$NAMESPACE.svc.cluster.local:8000"
fi
echo ""

# Function to make API call from inside cluster
make_request() {
    local endpoint=$1
    local description=$2

    echo -e "${BLUE}→ Testing: $description${NC}"
    echo "  Endpoint: $endpoint"

    if [[ -n "$GATEWAY_POD" ]]; then
        # Make request from inside the gateway pod
        RESPONSE=$(kubectl exec -n $NAMESPACE $GATEWAY_POD -- curl -s -w "\nHTTP_CODE:%{http_code}" "$API_URL$endpoint" 2>/dev/null || echo "FAILED")
    else
        # Make request from localhost
        RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}" "$API_URL$endpoint" 2>/dev/null || echo "FAILED")
    fi

    if [[ "$RESPONSE" == "FAILED" ]]; then
        echo -e "  ${YELLOW}⚠ Request failed${NC}"
    else
        HTTP_CODE=$(echo "$RESPONSE" | grep "HTTP_CODE" | cut -d: -f2)
        if [[ "$HTTP_CODE" == "200" ]] || [[ "$HTTP_CODE" == "401" ]] || [[ "$HTTP_CODE" == "404" ]]; then
            echo -e "  ${GREEN}✓ Response received (HTTP $HTTP_CODE)${NC}"
        else
            echo -e "  ${YELLOW}⚠ Unexpected response (HTTP $HTTP_CODE)${NC}"
        fi
    fi
    echo ""
    sleep 1
}

# Generate traffic to various endpoints
echo -e "${BLUE}Step 2: Generating Traffic to Services${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Health checks (should generate traces)
make_request "/health" "Gateway Health Check"
make_request "/api/health" "API Health Check"

# Auth service endpoints
make_request "/api/auth/health" "Auth Service Health"

# Tenant service endpoints
make_request "/api/tenants/health" "Tenant Service Health"

# Inventory service endpoints
make_request "/api/inventory/health" "Inventory Service Health"

# Orders service endpoints
make_request "/api/orders/health" "Orders Service Health"

# Forecasting service endpoints
make_request "/api/forecasting/health" "Forecasting Service Health"

echo -e "${BLUE}Step 3: Checking Service Logs for Telemetry${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Check a few service pods for tracing logs
SERVICES=("auth-service" "inventory-service" "gateway")

for service in "${SERVICES[@]}"; do
    POD=$(kubectl get pods -n $NAMESPACE -l app=$service --field-selector=status.phase=Running -o jsonpath='{.items[0].metadata.name}' 2>/dev/null)
    if [[ -n "$POD" ]]; then
        echo -e "${BLUE}Checking $service ($POD)...${NC}"
        TRACING_LOG=$(kubectl logs -n $NAMESPACE $POD --tail=100 2>/dev/null | grep -i "tracing\|otel" | head -n 2 || echo "")
        if [[ -n "$TRACING_LOG" ]]; then
            echo -e "${GREEN}✓ Tracing configured:${NC}"
            echo "$TRACING_LOG" | sed 's/^/  /'
        else
            echo -e "${YELLOW}⚠ No tracing logs found${NC}"
        fi
        echo ""
    fi
done

# Wait for data to be processed
echo -e "${BLUE}Step 4: Waiting for Data Processing${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Waiting 30 seconds for telemetry data to be processed..."
for i in {30..1}; do
    echo -ne "\r  ${i} seconds remaining..."
    sleep 1
done
echo -e "\n"

# Cleanup port-forward if started
if [[ -n "$PORT_FORWARD_PID" ]]; then
    kill $PORT_FORWARD_PID 2>/dev/null || true
fi

echo -e "${GREEN}✓ Test traffic generation complete!${NC}"
echo ""
echo -e "${BLUE}Next Steps:${NC}"
echo "1. Run the verification script to check for collected data:"
echo "   ./infrastructure/helm/verify-signoz-telemetry.sh"
echo ""
echo "2. Access SigNoz UI to visualize the data:"
echo "   https://monitoring.bakery-ia.local"
echo "   or"
echo "   kubectl port-forward -n bakery-ia svc/signoz 3301:8080"
echo "   Then go to: http://localhost:3301"
echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
