#!/bin/bash
# Run all PP-StructureV3 parameter tests
# Usage: ./backend/tests/run_ppstructure_tests.sh [test_type]
#   test_type: unit, api, e2e, performance, all (default: all)

set -e

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_ROOT="$( cd "$SCRIPT_DIR/../.." && pwd )"

cd "$PROJECT_ROOT"

# Activate virtual environment
if [ -f "$PROJECT_ROOT/venv/bin/activate" ]; then
    source "$PROJECT_ROOT/venv/bin/activate"
    echo "✓ Activated venv: $PROJECT_ROOT/venv"
else
    echo "⚠ Warning: venv not found at $PROJECT_ROOT/venv"
    echo "  Tests will use system Python environment"
fi

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

# Default test type
TEST_TYPE="${1:-all}"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}PP-StructureV3 Parameters Test Suite${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# Function to run tests
run_tests() {
    local test_name=$1
    local test_path=$2
    local markers=$3

    echo -e "${GREEN}Running ${test_name}...${NC}"

    if [ -n "$markers" ]; then
        pytest "$test_path" -v -m "$markers" --tb=short || {
            echo -e "${RED}✗ ${test_name} failed${NC}"
            return 1
        }
    else
        pytest "$test_path" -v --tb=short || {
            echo -e "${RED}✗ ${test_name} failed${NC}"
            return 1
        }
    fi

    echo -e "${GREEN}✓ ${test_name} passed${NC}"
    echo ""
}

# Run tests based on type
case "$TEST_TYPE" in
    unit)
        echo -e "${YELLOW}Running Unit Tests...${NC}"
        echo ""
        run_tests "Unit Tests" "backend/tests/services/test_ppstructure_params.py" ""
        ;;

    api)
        echo -e "${YELLOW}Running API Integration Tests...${NC}"
        echo ""
        run_tests "API Tests" "backend/tests/api/test_ppstructure_params_api.py" ""
        ;;

    e2e)
        echo -e "${YELLOW}Running E2E Tests...${NC}"
        echo ""
        echo -e "${YELLOW}⚠ Note: E2E tests require backend server running${NC}"
        echo -e "${YELLOW}⚠ Credentials: ymirliu@panjit.com.tw / 4RFV5tgb6yhn${NC}"
        echo ""
        run_tests "E2E Tests" "backend/tests/e2e/test_ppstructure_params_e2e.py" "e2e"
        ;;

    performance)
        echo -e "${YELLOW}Running Performance Tests...${NC}"
        echo ""
        run_tests "Performance Tests" "backend/tests/performance/test_ppstructure_params_performance.py" "performance"
        ;;

    all)
        echo -e "${YELLOW}Running All Tests...${NC}"
        echo ""

        # Unit tests
        run_tests "Unit Tests" "backend/tests/services/test_ppstructure_params.py" ""

        # API tests
        run_tests "API Tests" "backend/tests/api/test_ppstructure_params_api.py" ""

        # Performance tests
        run_tests "Performance Tests" "backend/tests/performance/test_ppstructure_params_performance.py" "performance"

        # E2E tests (optional, requires server)
        echo -e "${YELLOW}E2E Tests (requires server running)...${NC}"
        if curl -s http://localhost:8000/health > /dev/null 2>&1; then
            run_tests "E2E Tests" "backend/tests/e2e/test_ppstructure_params_e2e.py" "e2e"
        else
            echo -e "${YELLOW}⚠ Skipping E2E tests - server not running${NC}"
            echo -e "${YELLOW}  Start server with: cd backend && python -m uvicorn app.main:app${NC}"
            echo ""
        fi
        ;;

    *)
        echo -e "${RED}Invalid test type: $TEST_TYPE${NC}"
        echo "Usage: $0 [unit|api|e2e|performance|all]"
        exit 1
        ;;
esac

echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN}✓ All requested tests completed${NC}"
echo -e "${BLUE}========================================${NC}"

exit 0
