#!/bin/bash
set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
LOG_DIR="$SCRIPT_DIR/logs"
mkdir -p "$LOG_DIR"

echo "=== Chat Service Test Runner ==="
echo "Log directory: $LOG_DIR"
echo ""

cleanup() {
    echo ""
    echo "=== Collecting container logs ==="
    docker compose -f "$SCRIPT_DIR/docker-compose.yml" logs chat-api > "$LOG_DIR/chat-api.log" 2>&1 || true
    echo "=== Stopping containers ==="
    docker compose -f "$SCRIPT_DIR/docker-compose.yml" down --remove-orphans 2>/dev/null || true
}
trap cleanup EXIT

echo "=== Step 1: Building Docker images ==="
docker compose -f "$SCRIPT_DIR/docker-compose.yml" build 2>&1 | tee "$LOG_DIR/build.log"
echo ""

echo "=== Step 1.5: Building test tools ==="
(cd "$SCRIPT_DIR" && GOTOOLCHAIN=local GOPROXY=https://goproxy.cn,direct go build -o "$SCRIPT_DIR/wt_client" ./wt_client.go 2>&1) | tee -a "$LOG_DIR/build.log"
(cd "$SCRIPT_DIR" && GOTOOLCHAIN=local GOPROXY=https://goproxy.cn,direct go build -o "$SCRIPT_DIR/gen_token" ./gen_token.go 2>&1) | tee -a "$LOG_DIR/build.log"
echo ""

echo "=== Step 1.6: Generating JWT tokens ==="
JWT_SECRET="test-secret"
TOKEN_1001=$("$SCRIPT_DIR/gen_token" "$JWT_SECRET" 1001)
TOKEN_1002=$("$SCRIPT_DIR/gen_token" "$JWT_SECRET" 1002)
TOKEN_2001=$("$SCRIPT_DIR/gen_token" "$JWT_SECRET" 2001)
TOKEN_2002=$("$SCRIPT_DIR/gen_token" "$JWT_SECRET" 2002)
TOKEN_9001=$("$SCRIPT_DIR/gen_token" "$JWT_SECRET" 9001)
echo "  user 1001 token: ${TOKEN_1001:0:30}..."
echo "  user 1002 token: ${TOKEN_1002:0:30}..."
echo ""

echo "=== Step 2: Starting services ==="
docker compose -f "$SCRIPT_DIR/docker-compose.yml" up -d 2>&1 | tee -a "$LOG_DIR/build.log"
echo ""

echo "=== Step 3: Waiting for services to be ready ==="
for i in $(seq 1 30); do
    if curl -s http://localhost:28888 > /dev/null 2>&1 || [ $i -eq 30 ]; then
        break
    fi
    echo "  waiting... ($i/30)"
    sleep 2
done
sleep 3
echo "Services should be ready."
echo ""

VENV_PYTHON="$SCRIPT_DIR/.venv/bin/python3"
if [ ! -f "$VENV_PYTHON" ]; then
    echo "=== Creating Python venv ==="
    python3 -m venv "$SCRIPT_DIR/.venv"
    "$VENV_PYTHON" -m pip install websockets -q
fi

echo "=== Step 4: Running WebSocket tests ==="
cd "$SCRIPT_DIR"
ALL_PROXY= HTTP_PROXY= HTTPS_PROXY= NO_PROXY=* "$VENV_PYTHON" test_ws.py 2>&1 | tee "$LOG_DIR/ws_test_stdout.log"
WS_RC=${PIPESTATUS[0]}
echo ""

echo "=== Step 5: Running WebTransport fallback tests (WS) ==="
ALL_PROXY= HTTP_PROXY= HTTPS_PROXY= NO_PROXY=* "$VENV_PYTHON" test_wt.py 2>&1 | tee "$LOG_DIR/wt_test_stdout.log"
WT_FALLBACK_RC=${PIPESTATUS[0]}
echo ""

echo "=== Step 6: Running WebTransport native tests (QUIC) ==="
ALL_PROXY= HTTP_PROXY= HTTPS_PROXY= NO_PROXY=* "$SCRIPT_DIR/wt_client" "https://localhost:28443/wt/chat?token=${TOKEN_9001}" 2>&1 | tee "$LOG_DIR/wt_native_stdout.log"
WT_NATIVE_RC=${PIPESTATUS[0]}
echo ""

echo "=== Test Summary ==="
echo "WebSocket test:          $([ $WS_RC -eq 0 ] && echo 'PASSED' || echo 'FAILED')"
echo "WT fallback test (WS):   $([ $WT_FALLBACK_RC -eq 0 ] && echo 'PASSED' || echo 'FAILED')"
echo "WT native test (QUIC):   $([ $WT_NATIVE_RC -eq 0 ] && echo 'PASSED' || echo 'FAILED')"
echo ""
echo "Logs saved to: $LOG_DIR/"
ls -la "$LOG_DIR/"

if [ $WS_RC -ne 0 ] || [ $WT_FALLBACK_RC -ne 0 ] || [ $WT_NATIVE_RC -ne 0 ]; then
    exit 1
fi
