#!/bin/bash
set -e
# Clean up macOS metadata files that cause Python site module crashes
find . -name "._*" -delete 2>/dev/null || true


echo "=========================================="
echo "    CORTEX HUB INTEGRATION TESTS SETUP    "
echo "=========================================="

# Ensure Docker is in PATH for macOS
export PATH=$PATH:/usr/local/bin:/opt/homebrew/bin


# 1. Provide an .env if missing
if [ ! -f ".env" ]; then
    echo "Creating default .env for testing..."
    echo "SECRET_KEY=integration-secret-key-123" >> .env
    echo "SUPER_ADMINS=axieyangb@gmail.com" >> .env
    echo "GEMINI_API_KEY=YOUR_GEMINI_API_KEY" >> .env
    echo "GEMINI_MODEL_NAME=gemini-3-flash-preview" >> .env
fi

# Load and export .env variables
if [ -f ".env" ]; then
    export $(grep -v '^#' .env | xargs)
fi
export TTS_API_KEY=$GEMINI_API_KEY
export STT_API_KEY=$GEMINI_API_KEY
# export EMBEDDING_API_KEY=$GEMINI_API_KEY

# LOAD ENV FOR ALL SUBSEQUENT COMMANDS
set -a
source .env
set +a

export TTS_PROVIDER=mock
export STT_PROVIDER=mock
export MOCK_EVALUATION=true

# Load variables to use them in this script
export $(grep -v '^#' .env | xargs)

# Parse flags
NO_REBUILD=false
NATIVE_MODE=false
for arg in "$@"; do
    if [[ "$arg" == "--no-rebuild" ]]; then
        NO_REBUILD=true
    fi
    if [[ "$arg" == "--native" ]]; then
        NATIVE_MODE=true
    fi
done

# Check if docker daemon is reachable (i.e., not inside DevContainer without DIND) or if specifically skipped
DOCKER_AVAILABLE=false
if docker info >/dev/null 2>&1 && [ "$SKIP_DOCKER_NODES" != "true" ] && [ "$NATIVE_MODE" != "true" ]; then
    DOCKER_AVAILABLE=true
    export SKIP_DOCKER_NODES=false
else
    echo "Docker skipping, not reachable, or Native mode requested. Skipping Docker nodes..."
    export SKIP_DOCKER_NODES=true
    DOCKER_AVAILABLE=false
fi

if [ "$NO_REBUILD" = true ] && [ "$IS_RUNNING" = true ]; then
    echo "Service is already running and --no-rebuild flag provided."
    echo "Skipping rebuild and starting tests directly..."
else
    if [ "$DOCKER_AVAILABLE" = true ] && [ "$NATIVE_MODE" = false ]; then
        # 2. Cleanup existing test environment
        if docker info >/dev/null 2>&1; then
            echo "Purging existing test/dev environment..."
            # Stop default dev stack if running to avoid port 8002 conflict
            docker compose stop ai-frontend ai-hub browser-service 2>/dev/null || true
            docker compose -p cortexai down -v
        fi
        echo "Purging database and old containers..."
        docker compose down -v --remove-orphans
        docker kill test-node-1 test-node-2 2>/dev/null || true
        docker rm test-node-1 test-node-2 2>/dev/null || true

        # 3. Build & start the Hub stack via the unified start_server.sh script
        echo "Starting AI Hub mesh via ./start_server.sh..."
        if [ "$NO_REBUILD" = true ]; then
            bash ./start_server.sh
        else
            bash ./start_server.sh --rebuild
        fi

        # Wait for healthy
        echo "Waiting for AI Hub to be ready..."
        sleep 5
        until curl -I -s http://localhost:8002/api/v1/users/login/local | grep -q "405"; do
            echo "Waiting for AI Hub Backend..."
            sleep 2
        done
        sleep 3
        echo "AI Hub Backend is online."
    else
        # Start AI Hub Backend natively via uvicorn
        echo "Starting AI Hub natively in the background..."
        export DATABASE_URL="sqlite:///./data/ai-hub-test.db"
        export ENVIRONMENT="development"
        export PATH_PREFIX="/api/v1"
        # Purge local test database
        rm -f /tmp/cortex_hub_test.db
        mkdir -p ai-hub/data
        
        pkill -f uvicorn || true
        pkill -f agent_node.main || true
        sleep 1
        
        if [ -d "ai-hub" ]; then
            cd ai-hub
        elif [ -d "/app/ai-hub" ]; then
            cd /app/ai-hub
        fi
        # Pick unique ports and DB per run to avoid zombie process interference
        HUB_PORT=$(python3 -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()')
        GRPC_PORT=$(python3 -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()')
        DB_FILE="/tmp/integration_test_$(date +%s).db"
        LOG_FILE="/tmp/native_hub_${HUB_PORT}.log"
        export _INT_HUB_PORT=$HUB_PORT
        export _INT_GRPC_PORT=$GRPC_PORT
        if [ -f "../test_venv/bin/activate" ]; then source ../test_venv/bin/activate; elif [ -f "test_venv/bin/activate" ]; then source test_venv/bin/activate; elif [ -f "../venv/bin/activate" ]; then source ../venv/bin/activate; elif [ -f "venv/bin/activate" ]; then source venv/bin/activate; elif [ -f "../cortex-ai/bin/activate" ]; then source ../cortex-ai/bin/activate; elif [ -f "/tmp/venv2/bin/activate" ]; then source /tmp/venv2/bin/activate; elif [ -f "/tmp/venv/bin/activate" ]; then source /tmp/venv/bin/activate; else echo "No venv found for uvicorn"; fi
        echo "Starting Hub on HTTP=$HUB_PORT gRPC=$GRPC_PORT DB=$DB_FILE"
        PYTHONDONTWRITEBYTECODE=1 PYTHONPATH=.:../mesh-sdk GRPC_PORT=$GRPC_PORT DATA_DIR=/tmp/cortex-data SUPER_ADMINS=axieyangb@gmail.com CORTEX_ADMIN_PASSWORD=admin DATABASE_URL=sqlite:///$DB_FILE AGENT_NODE_SRC_DIR=../agent-node SKILLS_SRC_DIR=../skills python3 -B -m uvicorn app.main:app --host 0.0.0.0 --port $HUB_PORT > $LOG_FILE 2>&1 &
        HUB_PID=$!
        cd - > /dev/null
        
        # Wait for healthy
        echo "Waiting for AI Hub to be ready..."
        sleep 2
        until curl -I -s http://localhost:${_INT_HUB_PORT}/api/v1/users/login/local | grep -q "405"; do
            echo "Waiting for AI Hub Backend natively..."
            sleep 2
        done
        sleep 5
        echo "AI Hub Backend is online on port ${_INT_HUB_PORT}."
    fi
fi

# 4. User setup via Pytest fixtures
echo "=========================================="
echo "    EXECUTING E2E INTEGRATION SUITE       "
echo "=========================================="
if [ "$NATIVE_MODE" = true ] || [ "$NATIVE" = 1 ] || [ "$DOCKER_AVAILABLE" = false ]; then
    export SYNC_TEST_BASE_URL="http://127.0.0.1:${_INT_HUB_PORT}/api/v1"
    export TEST_HUB_URL="http://127.0.0.1:${_INT_HUB_PORT}"
    export TEST_GRPC_ENDPOINT="127.0.0.1:${_INT_GRPC_PORT}"
    export CORTEX_ADMIN_PASSWORD="admin"
    export SUPER_ADMINS="axieyangb@gmail.com"
else
    export SYNC_TEST_BASE_URL="http://127.0.0.1:8002/api/v1"
    export TEST_HUB_URL="http://127.0.0.1:8002"
    export TEST_GRPC_ENDPOINT="127.0.0.1:50051"
    export CORTEX_ADMIN_PASSWORD="admin"
    export SUPER_ADMINS="axieyangb@gmail.com"
fi
if [ -f "test_venv/bin/activate" ]; then source test_venv/bin/activate; elif [ -f "cortex-ai/bin/activate" ]; then source cortex-ai/bin/activate; elif [ -f "/tmp/venv2/bin/activate" ]; then source /tmp/venv2/bin/activate; elif [ -f "venv/bin/activate" ]; then source venv/bin/activate; elif [ -f "/tmp/venv/bin/activate" ]; then source /tmp/venv/bin/activate; else echo "No venv found, hoping pytest is in global PATH."; fi

# Determine the best python to use
VENV_PYTHON="python3"
if [ -f "test_venv/bin/python3" ]; then
    VENV_PYTHON="./test_venv/bin/python3"
elif [ -f "cortex-ai/bin/python3" ] && [ -x "cortex-ai/bin/python3" ]; then
    VENV_PYTHON="./cortex-ai/bin/python3"
elif [ -f "venv/bin/python3" ]; then
    VENV_PYTHON="./venv/bin/python3"
fi



TEST_TARGETS=()
for arg in "$@"; do
    if [[ "$arg" != "--no-rebuild" ]] && [[ "$arg" != "--native" ]]; then
        TEST_TARGETS+=("$arg")
    fi
done

if [ ${#TEST_TARGETS[@]} -eq 0 ]; then
    TEST_TARGETS=("ai-hub/integration_tests/")
fi

    PYTHONPATH=ai-hub:mesh-sdk $VENV_PYTHON -m pytest "${TEST_TARGETS[@]}" -v -p no:cacheprovider



if [ "$NO_REBUILD" = false ] || [ "$IS_RUNNING" = false ]; then
    echo "=========================================="
    echo "    TEARING DOWN INTEGRATION ENVIRONMENT  "
    if [ "$DOCKER_AVAILABLE" = true ]; then
        docker compose down -v
    else
        kill $HUB_PID || true
    fi
    echo "Done!"
else
    echo "=========================================="
    echo " SKIPPING TEARDOWN DUE TO --no-rebuild    "
    echo "=========================================="
    echo "Done!"
fi
