#!/usr/bin/env bash
set -euo pipefail

repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
python_bin="$repo_dir/server/.venv/bin/python"

cxx_test="$(mktemp)"
c++ -std=c++17 -Wall -Wextra -Werror -I "$repo_dir/firmware/main" \
    "$repo_dir/firmware/tests/wifi_profile_selection_test.cpp" -o "$cxx_test"
"$cxx_test"
c++ -std=c++17 -Wall -Wextra -Werror -I "$repo_dir/boards/common/include" \
    "$repo_dir/firmware/tests/speech_diagnostics_test.cpp" -o "$cxx_test"
"$cxx_test"
c++ -std=c++17 -Wall -Wextra -Werror -I "$repo_dir/boards/common/include" \
    "$repo_dir/firmware/tests/approval_control_test.cpp" -o "$cxx_test"
"$cxx_test"
c++ -std=c++17 -Wall -Wextra -Werror -I "$repo_dir/boards/common/include" \
    "$repo_dir/firmware/tests/button_gesture_test.cpp" -o "$cxx_test"
"$cxx_test"
c++ -std=c++17 -Wall -Wextra -Werror -I "$repo_dir/boards/common/include" \
    "$repo_dir/firmware/tests/speech_control_test.cpp" -o "$cxx_test"
"$cxx_test"
c++ -std=c++17 -Wall -Wextra -Werror -I "$repo_dir/boards/common/include" \
    "$repo_dir/firmware/tests/physical_input_test.cpp" -o "$cxx_test"
"$cxx_test"
rm -f "$cxx_test"

cxx_test="$(mktemp)"
c++ -std=c++17 -Wall -Wextra -Werror -I "$repo_dir/firmware/main" \
    "$repo_dir/firmware/tests/websocket_text_receiver_test.cpp" -o "$cxx_test"
"$cxx_test"
rm -f "$cxx_test"
python3 "$repo_dir/firmware/tests/run_agent_view_test.py"

node --test --experimental-strip-types \
    "$repo_dir/integrations/pi/device-lab-pager.test.ts" \
    "$repo_dir/integrations/pi/control-socket.test.ts" \
    "$repo_dir/integrations/pi/approval.test.ts" \
    "$repo_dir/server/tests/admin.test.mjs"

if [[ ! -x "$python_bin" ]]; then
    "$repo_dir/scripts/setup-server.sh"
fi

cd "$repo_dir/server"
"$python_bin" -m pytest -q

log_file="$(mktemp)"
credential_file="$(mktemp)"
state_file="$(mktemp)"
runtime_dir="$(mktemp -d)"
test_token="test-token-for-local-smoke-only"
test_pi_token="test-pi-token-for-local-smoke-only"
printf '{"device_id":"smoke-device","device_token":"%s","pi_token":"%s"}\n' \
    "$test_token" "$test_pi_token" >"$credential_file"
test_port="$("$python_bin" -c 'import socket; s = socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1]); s.close()')"
DEVICE_LAB_CONTROL_DIR="$runtime_dir" DEVICE_CREDENTIALS_FILE="$credential_file" GATEWAY_STATE_FILE="$state_file" \
    GATEWAY_HOST=127.0.0.1 GATEWAY_PORT="$test_port" \
    "$repo_dir/scripts/run-server.sh" >"$log_file" 2>&1 &
server_pid=$!
cleanup() {
    kill "$server_pid" >/dev/null 2>&1 || true
    wait "$server_pid" >/dev/null 2>&1 || true
    rm -f "$log_file"
    rm -f "$credential_file"
    rm -f "$state_file"
    rmdir "$runtime_dir"
}
trap cleanup EXIT

ready=false
for _ in {1..100}; do
    if "$python_bin" -c "import httpx; httpx.get('http://127.0.0.1:$test_port/health', timeout=0.2).raise_for_status()" >/dev/null 2>&1; then
        ready=true
        break
    fi
    sleep 0.1
done

if [[ "$ready" != true ]]; then
    echo "Gateway did not become ready:" >&2
    sed -n '1,200p' "$log_file" >&2
    exit 1
fi

GATEWAY_HTTP_URL="http://127.0.0.1:$test_port" \
GATEWAY_WS_URL="ws://127.0.0.1:$test_port/device/ws" \
GATEWAY_TEST_DEVICE_TOKEN="$test_token" \
GATEWAY_TEST_PI_TOKEN="$test_pi_token" \
    "$python_bin" tests/smoke_client.py

"$python_bin" tests/smoke_voice.py
