#!/bin/bash

# Production E2E Testing Script for HVAC Plugin
# Tests all custom functionality on https://upskillhvac.com/

set -e

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

echo -e "${BLUE}🚀 HVAC Plugin Production E2E Test Suite${NC}"
echo -e "${BLUE}=========================================${NC}"
echo ""
echo -e "${YELLOW}Target Environment:${NC} https://upskillhvac.com/"
echo -e "${YELLOW}Test Suite:${NC} Complete HVAC Plugin Functionality"
echo ""

# Check if we're in the right directory
if [ ! -f "package.json" ] || [ ! -d "tests/e2e" ]; then
    echo -e "${RED}❌ Error: Must be run from project root directory${NC}"
    echo "Expected files: package.json, tests/e2e/"
    exit 1
fi

# Check if Playwright is installed
if ! npx playwright --version > /dev/null 2>&1; then
    echo -e "${RED}❌ Error: Playwright not found${NC}"
    echo "Please install Playwright:"
    echo "  npm install @playwright/test"
    echo "  npx playwright install"
    exit 1
fi

# Check environment variables
echo -e "${YELLOW}🔍 Checking test environment...${NC}"

if [ -z "$PROD_TRAINER_EMAIL" ] || [ -z "$PROD_TRAINER_PASSWORD" ]; then
    echo -e "${YELLOW}⚠️ Warning: Production trainer credentials not set${NC}"
    echo "  Some authenticated tests will be skipped"
    echo "  To run full test suite, set:"
    echo "    export PROD_TRAINER_EMAIL=your-trainer@email.com"
    echo "    export PROD_TRAINER_PASSWORD=your-password"
    echo ""
else
    echo -e "${GREEN}✅ Trainer credentials available${NC}"
fi

if [ -z "$PROD_MASTER_TRAINER_EMAIL" ] || [ -z "$PROD_MASTER_TRAINER_PASSWORD" ]; then
    echo -e "${YELLOW}ℹ️ Master trainer credentials not set (optional)${NC}"
    echo ""
else
    echo -e "${GREEN}✅ Master trainer credentials available${NC}"
fi

# Create test results directory
mkdir -p test-results

# Install browsers if needed
echo -e "${YELLOW}🌐 Ensuring browsers are installed...${NC}"
npx playwright install chromium firefox webkit

echo ""
echo -e "${BLUE}📋 Test Categories:${NC}"
echo "  1. Public Site Functionality"
echo "  2. Trainer Authentication & Registration"
echo "  3. Training Leads Management System"
echo "  4. Trainer Dashboard & Navigation"
echo "  5. Event Creation & Management"
echo "  6. Certificate Generation System"
echo "  7. Find a Trainer Public Features"
echo "  8. Master Trainer Functionality"
echo "  9. Documentation System"
echo "  10. Profile Management"
echo "  11. Security & Session Handling"
echo "  12. Mobile & Tablet Responsiveness"
echo ""

# Prompt for confirmation
echo -e "${YELLOW}⚠️ This will run tests against the PRODUCTION site${NC}"
read -p "Continue? (y/N): " confirm

if [[ $confirm != [yY] && $confirm != [yY][eE][sS] ]]; then
    echo "Test run cancelled."
    exit 0
fi

echo ""
echo -e "${GREEN}🎬 Starting production test suite...${NC}"
echo ""

# Run the tests with production configuration
echo -e "${BLUE}Running comprehensive production tests...${NC}"

npx playwright test \
    --config=tests/e2e/playwright.production.config.ts \
    --reporter=html,line,json \
    --output=test-results/production-output \
    --workers=1 \
    --timeout=60000 \
    --retries=1

TEST_EXIT_CODE=$?

echo ""
echo -e "${BLUE}📊 Test Results Summary${NC}"
echo -e "${BLUE}=======================${NC}"

if [ $TEST_EXIT_CODE -eq 0 ]; then
    echo -e "${GREEN}🎉 All tests passed successfully!${NC}"
else
    echo -e "${YELLOW}⚠️ Some tests failed or had issues${NC}"
fi

# Display results information
echo ""
echo -e "${YELLOW}📁 Test Artifacts:${NC}"
echo "  HTML Report: test-results/production-html-report/"
echo "  JSON Results: test-results/production-results.json"
echo "  Screenshots: test-results/ (failure-*.png)"
echo "  Videos: test-results/ (failure-*.webm)"
echo "  Test Summary: test-results/production-test-summary.txt"

echo ""
echo -e "${YELLOW}🔗 Quick Commands:${NC}"
echo "  View HTML Report:"
echo "    npx playwright show-report test-results/production-html-report/"
echo ""
echo "  View failure trace (if any failures):"
echo "    npx playwright show-trace test-results/[trace-file].zip"
echo ""
echo "  Rerun only failed tests:"
echo "    npx playwright test --config=tests/e2e/playwright.production.config.ts --last-failed"
echo ""

# Open HTML report if tests completed
if command -v open > /dev/null 2>&1 && [ -d "test-results/production-html-report" ]; then
    echo -e "${YELLOW}Opening HTML report...${NC}"
    npx playwright show-report test-results/production-html-report/ &
fi

echo -e "${BLUE}✨ Production testing completed${NC}"

# Return the test exit code
exit $TEST_EXIT_CODE