#!/bin/bash

# Contact Form Test Runner
# Quick access to common test scenarios

set -e

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

echo -e "${GREEN}Contact Form Test Runner${NC}"
echo "======================================"
echo ""

# Check if app is running
if ! curl -s http://localhost:9000 > /dev/null; then
    echo -e "${RED}Error: Application not running on port 9000${NC}"
    echo "Please start the app with: npm run dev"
    exit 1
fi

# Check Supabase connection (optional)
if [ -z "$NEXT_PUBLIC_SUPABASE_URL" ]; then
    echo -e "${YELLOW}Warning: NEXT_PUBLIC_SUPABASE_URL not set${NC}"
    echo "Database tests may be skipped"
    echo ""
fi

# Parse command line arguments
SUITE="all"
BROWSER="chromium"
MODE="run"

while [[ $# -gt 0 ]]; do
    case $1 in
        --suite)
            SUITE="$2"
            shift 2
            ;;
        --browser)
            BROWSER="$2"
            shift 2
            ;;
        --debug)
            MODE="debug"
            shift
            ;;
        --ui)
            MODE="ui"
            shift
            ;;
        --headed)
            MODE="headed"
            shift
            ;;
        --help)
            echo "Usage: ./tests/run-contact-tests.sh [OPTIONS]"
            echo ""
            echo "Options:"
            echo "  --suite <name>     Run specific test suite:"
            echo "                     - all (default)"
            echo "                     - form"
            echo "                     - database"
            echo "                     - api"
            echo "                     - arabic"
            echo "                     - accessibility"
            echo "                     - errors"
            echo "  --browser <name>   Run on specific browser:"
            echo "                     - chromium (default)"
            echo "                     - firefox"
            echo "                     - webkit"
            echo "                     - mobile-chrome"
            echo "                     - mobile-safari"
            echo "  --debug            Run in debug mode"
            echo "  --ui               Run in UI mode"
            echo "  --headed           Run in headed mode (show browser)"
            echo "  --help             Show this help message"
            echo ""
            echo "Examples:"
            echo "  ./tests/run-contact-tests.sh --suite form"
            echo "  ./tests/run-contact-tests.sh --suite database --browser firefox"
            echo "  ./tests/run-contact-tests.sh --debug"
            exit 0
            ;;
        *)
            echo -e "${RED}Unknown option: $1${NC}"
            echo "Use --help for usage information"
            exit 1
            ;;
    esac
done

# Determine test filter
GREP_PATTERN=""
case $SUITE in
    form)
        GREP_PATTERN="Form Functionality"
        echo -e "Running: ${GREEN}Form Functionality Tests${NC}"
        ;;
    database)
        GREP_PATTERN="Database Integration"
        echo -e "Running: ${GREEN}Database Integration Tests${NC}"
        ;;
    api)
        GREP_PATTERN="API Endpoint Testing"
        echo -e "Running: ${GREEN}API Endpoint Tests${NC}"
        ;;
    arabic)
        GREP_PATTERN="Bilingual Support"
        echo -e "Running: ${GREEN}Bilingual Support Tests${NC}"
        ;;
    accessibility)
        GREP_PATTERN="Accessibility"
        echo -e "Running: ${GREEN}Accessibility Tests${NC}"
        ;;
    errors)
        GREP_PATTERN="Error Handling"
        echo -e "Running: ${GREEN}Error Handling Tests${NC}"
        ;;
    all)
        echo -e "Running: ${GREEN}All Contact Form Tests${NC}"
        ;;
    *)
        echo -e "${RED}Unknown suite: $SUITE${NC}"
        echo "Use --help for available suites"
        exit 1
        ;;
esac

# Determine browser project
PROJECT_NAME=""
case $BROWSER in
    chromium)
        PROJECT_NAME="chromium"
        ;;
    firefox)
        PROJECT_NAME="firefox"
        ;;
    webkit)
        PROJECT_NAME="webkit"
        ;;
    mobile-chrome)
        PROJECT_NAME="Mobile Chrome"
        ;;
    mobile-safari)
        PROJECT_NAME="Mobile Safari"
        ;;
    *)
        echo -e "${RED}Unknown browser: $BROWSER${NC}"
        exit 1
        ;;
esac

echo "Browser: ${BROWSER}"
echo ""

# Build test command
BASE_CMD="npx playwright test tests/contact-form.spec.ts"

if [ "$MODE" = "debug" ]; then
    CMD="$BASE_CMD --debug"
elif [ "$MODE" = "ui" ]; then
    CMD="$BASE_CMD --ui"
elif [ "$MODE" = "headed" ]; then
    CMD="$BASE_CMD --headed"
else
    CMD="$BASE_CMD"
fi

# Add project filter
CMD="$CMD --project=\"$PROJECT_NAME\""

# Add grep pattern if specified
if [ -n "$GREP_PATTERN" ]; then
    CMD="$CMD -g \"$GREP_PATTERN\""
fi

# Run tests
echo "Executing: $CMD"
echo ""
eval $CMD

# Check exit code
if [ $? -eq 0 ]; then
    echo ""
    echo -e "${GREEN}✓ All tests passed!${NC}"
    exit 0
else
    echo ""
    echo -e "${RED}✗ Some tests failed${NC}"
    echo "View detailed report: npm run test:e2e:report"
    exit 1
fi
