#!/bin/bash

# Run the enhanced trainer journey test with certificate functionality
# This script runs the trainer-journey-with-certificates.test.ts file
# with proper environment setup

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

# Change to the project root directory
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$SCRIPT_DIR/.." || exit 1
echo "Changed working directory to: $(pwd)"

# Display banner
echo -e "${BLUE}============================================${NC}"
echo -e "${BLUE}  Trainer Journey with Certificates Test   ${NC}"
echo -e "${BLUE}============================================${NC}"

# Load environment variables
if [ -f ./.env ]; then
  echo "Loading environment variables from .env"
  source ./.env
else
  echo -e "${RED}Error: .env file not found!${NC}"
  echo "Make sure you have a .env file with the required environment variables"
  exit 1
fi

# Verify plugin is activated
echo -e "${YELLOW}Verifying plugin activation...${NC}"
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp plugin is-active hvac-community-events --allow-root"

if [ $? -ne 0 ]; then
  echo -e "${YELLOW}Plugin not active. Activating hvac-community-events...${NC}"
  sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp plugin activate hvac-community-events --allow-root"
  
  if [ $? -ne 0 ]; then
    echo -e "${RED}Failed to activate plugin. Cannot continue.${NC}"
    exit 1
  fi
  
  # Flush rewrite rules
  echo -e "${YELLOW}Flushing rewrite rules...${NC}"
  sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp rewrite flush --hard --allow-root"
else
  echo -e "${GREEN}Plugin is active.${NC}"
fi

# Check if test user exists
echo -e "${YELLOW}Checking test user...${NC}"
USER_EXISTS=$(sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp user get test_trainer --field=ID --allow-root 2>/dev/null || echo 'notfound'")

if [ "$USER_EXISTS" == "notfound" ]; then
  echo -e "${YELLOW}Test user not found. Creating test user...${NC}"
  # Run the setup script
  ./bin/setup-staging-test-users.sh
else
  echo -e "${GREEN}Test user found with ID: $USER_EXISTS${NC}"
fi

# Clear cache if needed
echo -e "${YELLOW}Clearing cache...${NC}"
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp cache flush --allow-root"

# Get UI mode from args
UI_MODE="--headed"
if [[ "$1" == "--headless" ]]; then
  UI_MODE="--headless"
  echo -e "${YELLOW}Running in headless mode${NC}"
else
  echo -e "${YELLOW}Running with UI visible${NC}"
fi

# Get debug mode from args
DEBUG_MODE=""
if [[ "$1" == "--debug" || "$2" == "--debug" ]]; then
  DEBUG_MODE="--debug"
  echo -e "${YELLOW}Running in debug mode${NC}"
fi

# Run the test
echo -e "${YELLOW}Running trainer journey with certificates test...${NC}"
echo -e "${YELLOW}$ npx playwright test tests/e2e/trainer-journey-with-certificates.test.ts $UI_MODE $DEBUG_MODE${NC}"

# Start the test
npx playwright test tests/e2e/trainer-journey-with-certificates.test.ts $UI_MODE $DEBUG_MODE

# Check test result
TEST_RESULT=$?
if [ $TEST_RESULT -eq 0 ]; then
  echo -e "${GREEN}✓ Test completed successfully!${NC}"
else
  echo -e "${RED}✗ Test failed with exit code $TEST_RESULT${NC}"
fi

# Show report location
echo -e "${YELLOW}Test artifacts are available in the test-results directory${NC}"
echo -e "${YELLOW}To view HTML report, run: npx playwright show-report${NC}"

exit $TEST_RESULT