#!/bin/bash

# Contact Form Test Environment Checker
# Verifies all prerequisites are met before running tests

set -e

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

echo -e "${BLUE}======================================${NC}"
echo -e "${BLUE}Contact Form Test Environment Checker${NC}"
echo -e "${BLUE}======================================${NC}"
echo ""

ERRORS=0
WARNINGS=0

# Check 1: Application running
echo -n "Checking if application is running on port 9000... "
if curl -s http://localhost:9000 > /dev/null 2>&1; then
    echo -e "${GREEN}✓ OK${NC}"
else
    echo -e "${RED}✗ FAILED${NC}"
    echo -e "${YELLOW}  → Start the application with: npm run dev${NC}"
    ERRORS=$((ERRORS + 1))
fi

# Check 2: Node.js version
echo -n "Checking Node.js version... "
NODE_VERSION=$(node --version | cut -d'v' -f2 | cut -d'.' -f1)
if [ "$NODE_VERSION" -ge 18 ]; then
    echo -e "${GREEN}✓ OK (v$(node --version | cut -d'v' -f2))${NC}"
else
    echo -e "${RED}✗ FAILED (Node.js 18+ required)${NC}"
    ERRORS=$((ERRORS + 1))
fi

# Check 3: npm dependencies
echo -n "Checking if dependencies are installed... "
if [ -d "node_modules" ] && [ -f "node_modules/.package-lock.json" ]; then
    echo -e "${GREEN}✓ OK${NC}"
else
    echo -e "${RED}✗ FAILED${NC}"
    echo -e "${YELLOW}  → Install dependencies with: npm install${NC}"
    ERRORS=$((ERRORS + 1))
fi

# Check 4: Playwright installed
echo -n "Checking Playwright installation... "
if [ -f "node_modules/.bin/playwright" ]; then
    echo -e "${GREEN}✓ OK${NC}"
else
    echo -e "${RED}✗ FAILED${NC}"
    echo -e "${YELLOW}  → Install Playwright with: npx playwright install${NC}"
    ERRORS=$((ERRORS + 1))
fi

# Check 5: Playwright browsers
echo -n "Checking Playwright browsers... "
if npx playwright install --dry-run 2>&1 | grep -q "Skipping"; then
    echo -e "${GREEN}✓ OK (all browsers installed)${NC}"
else
    echo -e "${YELLOW}⚠ WARNING (browsers may need installation)${NC}"
    echo -e "${YELLOW}  → Install with: npx playwright install${NC}"
    WARNINGS=$((WARNINGS + 1))
fi

# Check 6: Environment variables - Supabase
echo -n "Checking Supabase configuration... "
if [ -n "$NEXT_PUBLIC_SUPABASE_URL" ] && [ -n "$NEXT_PUBLIC_SUPABASE_ANON_KEY" ]; then
    echo -e "${GREEN}✓ OK${NC}"

    # Try to connect to Supabase
    echo -n "  Testing Supabase connection... "
    if curl -s "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/" -H "apikey: $NEXT_PUBLIC_SUPABASE_ANON_KEY" > /dev/null 2>&1; then
        echo -e "${GREEN}✓ Connected${NC}"
    else
        echo -e "${YELLOW}⚠ Cannot connect${NC}"
        echo -e "${YELLOW}    Database tests may be skipped${NC}"
        WARNINGS=$((WARNINGS + 1))
    fi
else
    echo -e "${YELLOW}⚠ NOT SET${NC}"
    echo -e "${YELLOW}  → Database tests will be skipped${NC}"
    echo -e "${YELLOW}  → Set in .env.local:${NC}"
    echo -e "${YELLOW}    NEXT_PUBLIC_SUPABASE_URL=http://127.0.0.1:54321${NC}"
    echo -e "${YELLOW}    NEXT_PUBLIC_SUPABASE_ANON_KEY=your-key${NC}"
    WARNINGS=$((WARNINGS + 1))
fi

# Check 7: Supabase Service Role Key
echo -n "Checking Supabase Service Role Key... "
if [ -n "$SUPABASE_SERVICE_ROLE_KEY" ]; then
    echo -e "${GREEN}✓ OK${NC}"
else
    echo -e "${YELLOW}⚠ NOT SET${NC}"
    echo -e "${YELLOW}  → Database cleanup may fail${NC}"
    echo -e "${YELLOW}  → Set in .env.local: SUPABASE_SERVICE_ROLE_KEY=your-service-key${NC}"
    WARNINGS=$((WARNINGS + 1))
fi

# Check 8: Redis/Upstash for rate limiting
echo -n "Checking Redis configuration... "
if [ -n "$UPSTASH_REDIS_REST_URL" ] && [ -n "$UPSTASH_REDIS_REST_TOKEN" ]; then
    echo -e "${GREEN}✓ OK${NC}"
else
    echo -e "${YELLOW}⚠ NOT SET${NC}"
    echo -e "${YELLOW}  → Rate limiting tests may fail${NC}"
    echo -e "${YELLOW}  → Set in .env.local:${NC}"
    echo -e "${YELLOW}    UPSTASH_REDIS_REST_URL=your-redis-url${NC}"
    echo -e "${YELLOW}    UPSTASH_REDIS_REST_TOKEN=your-token${NC}"
    WARNINGS=$((WARNINGS + 1))
fi

# Check 9: Test files exist
echo -n "Checking test files... "
if [ -f "tests/contact-form.spec.ts" ] && [ -f "tests/helpers/supabase-test-utils.ts" ]; then
    echo -e "${GREEN}✓ OK${NC}"
else
    echo -e "${RED}✗ FAILED${NC}"
    echo -e "${YELLOW}  → Test files are missing${NC}"
    ERRORS=$((ERRORS + 1))
fi

# Check 10: Disk space
echo -n "Checking disk space... "
if [ "$(uname)" == "Darwin" ]; then
    # macOS
    AVAILABLE_GB=$(df -h . | awk 'NR==2 {print $4}' | sed 's/Gi//')
else
    # Linux
    AVAILABLE_GB=$(df -BG . | awk 'NR==2 {print $4}' | sed 's/G//')
fi

if [ "$AVAILABLE_GB" -gt 5 ]; then
    echo -e "${GREEN}✓ OK (${AVAILABLE_GB}GB available)${NC}"
else
    echo -e "${YELLOW}⚠ LOW (${AVAILABLE_GB}GB available)${NC}"
    echo -e "${YELLOW}  → Playwright may need more space for screenshots/videos${NC}"
    WARNINGS=$((WARNINGS + 1))
fi

# Check 11: Port availability
echo -n "Checking port 9000 is accessible... "
if nc -z localhost 9000 2>/dev/null; then
    echo -e "${GREEN}✓ OK${NC}"
else
    echo -e "${RED}✗ FAILED${NC}"
    echo -e "${YELLOW}  → Port 9000 is not accessible${NC}"
    ERRORS=$((ERRORS + 1))
fi

# Check 12: Contact page accessibility
echo -n "Checking contact page loads... "
if curl -s http://localhost:9000/en/contact | grep -q "contact\|form" 2>/dev/null; then
    echo -e "${GREEN}✓ OK${NC}"
else
    echo -e "${YELLOW}⚠ WARNING${NC}"
    echo -e "${YELLOW}  → Contact page may not be working correctly${NC}"
    WARNINGS=$((WARNINGS + 1))
fi

# Summary
echo ""
echo -e "${BLUE}======================================${NC}"
echo -e "${BLUE}Summary${NC}"
echo -e "${BLUE}======================================${NC}"

if [ $ERRORS -eq 0 ] && [ $WARNINGS -eq 0 ]; then
    echo -e "${GREEN}✓ All checks passed!${NC}"
    echo -e "${GREEN}  Environment is ready for testing.${NC}"
    echo ""
    echo -e "Run tests with:"
    echo -e "  ${BLUE}npm run test:e2e -- contact-form.spec.ts${NC}"
    echo ""
    exit 0
elif [ $ERRORS -eq 0 ]; then
    echo -e "${YELLOW}⚠ ${WARNINGS} warning(s) found${NC}"
    echo -e "${YELLOW}  Tests will run, but some may be skipped.${NC}"
    echo ""
    echo -e "Run tests with:"
    echo -e "  ${BLUE}npm run test:e2e -- contact-form.spec.ts${NC}"
    echo ""
    exit 0
else
    echo -e "${RED}✗ ${ERRORS} error(s) found${NC}"
    if [ $WARNINGS -gt 0 ]; then
        echo -e "${YELLOW}⚠ ${WARNINGS} warning(s) found${NC}"
    fi
    echo -e "${RED}  Please fix the errors above before running tests.${NC}"
    echo ""
    exit 1
fi
