#!/bin/bash

# Email Attendees Feature Tests Runner
# This script:
# 1. Transfers test files to the staging server
# 2. Runs the tests on the staging server
# 3. Returns the results

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

# Configuration
STAGING_HOST="146.190.76.204"
STAGING_USER="roodev"
REMOTE_PLUGIN_PATH="/home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events"
LOCAL_PATH="$(pwd)"

# Print header
echo -e "${GREEN}=== Running Email Attendees Tests on Staging ===${NC}"
echo -e "Remote host: ${STAGING_HOST}"
echo -e "Remote user: ${STAGING_USER}"
echo -e "Plugin path: ${REMOTE_PLUGIN_PATH}"
echo -e "======================================="

# Check if test directory exists on staging
ssh ${STAGING_USER}@${STAGING_HOST} "if [ ! -d ${REMOTE_PLUGIN_PATH}/tests ]; then mkdir -p ${REMOTE_PLUGIN_PATH}/tests; fi"
ssh ${STAGING_USER}@${STAGING_HOST} "if [ ! -d ${REMOTE_PLUGIN_PATH}/tests/unit ]; then mkdir -p ${REMOTE_PLUGIN_PATH}/tests/unit; fi"
ssh ${STAGING_USER}@${STAGING_HOST} "if [ ! -d ${REMOTE_PLUGIN_PATH}/tests/integration ]; then mkdir -p ${REMOTE_PLUGIN_PATH}/tests/integration; fi"

# Transfer test files
echo -e "Transferring test files to staging server..."
scp "${LOCAL_PATH}/wordpress/wp-content/plugins/hvac-community-events/tests/unit/test-email-attendees-data.php" ${STAGING_USER}@${STAGING_HOST}:${REMOTE_PLUGIN_PATH}/tests/unit/
scp "${LOCAL_PATH}/wordpress/wp-content/plugins/hvac-community-events/tests/integration/test-email-attendees-integration.php" ${STAGING_USER}@${STAGING_HOST}:${REMOTE_PLUGIN_PATH}/tests/integration/

# Create bootstrap file if it doesn't exist
cat > /tmp/bootstrap-staging.php << 'EOF'
<?php
/**
 * PHPUnit bootstrap file for staging tests
 */

// Load WordPress test environment
$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
    $_tests_dir = '/tmp/wordpress-tests-lib';
}

// Give access to tests_add_filter() function
require_once $_tests_dir . '/includes/functions.php';

/**
 * Manually load the plugin being tested
 */
function _manually_load_plugin() {
    // First load The Events Calendar and other dependencies
    require '/home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/the-events-calendar/the-events-calendar.php';
    require '/home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/event-tickets/event-tickets.php';
    
    // Then load our plugin
    require '/home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/hvac-community-events.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

// Start up the WP testing environment
require $_tests_dir . '/includes/bootstrap.php';
EOF

# Transfer bootstrap file
scp /tmp/bootstrap-staging.php ${STAGING_USER}@${STAGING_HOST}:${REMOTE_PLUGIN_PATH}/tests/

# Create test config file
cat > /tmp/wp-tests-config-staging.php << 'EOF'
<?php
/**
 * WordPress Test Suite configuration for staging.
 */

// Test with WordPress debug mode on
define( 'WP_DEBUG', true );

// Path to WordPress core
define( 'ABSPATH', '/home/974670.cloudwaysapps.com/uberrxmprk/public_html/' );

// Test database config
define( 'DB_NAME', 'uberrxmprk' );
define( 'DB_USER', 'uberrxmprk' );
define( 'DB_PASSWORD', '89FqzFg9vG' );
define( 'DB_HOST', 'localhost' );
define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', '' );

// WordPress DB table prefix
$table_prefix = 'wp_';

// Test with multisite disabled
define( 'WP_TESTS_MULTISITE', false );

// Disable automatic updates
define( 'AUTOMATIC_UPDATER_DISABLED', true );

define( 'WP_DEBUG_LOG', false );
define( 'WP_DEBUG_DISPLAY', false );
define( 'SCRIPT_DEBUG', false );
define( 'SAVEQUERIES', false );
EOF

# Transfer config file
scp /tmp/wp-tests-config-staging.php ${STAGING_USER}@${STAGING_HOST}:${REMOTE_PLUGIN_PATH}/tests/

# Run the tests on staging
echo -e "Running tests on staging server..."
ssh ${STAGING_USER}@${STAGING_HOST} "cd ${REMOTE_PLUGIN_PATH} && php vendor/bin/phpunit --bootstrap tests/bootstrap-staging.php tests/unit/test-email-attendees-data.php"
echo -e "---------------------------------------"
ssh ${STAGING_USER}@${STAGING_HOST} "cd ${REMOTE_PLUGIN_PATH} && php vendor/bin/phpunit --bootstrap tests/bootstrap-staging.php tests/integration/test-email-attendees-integration.php"

# Run E2E tests if available
echo -e "---------------------------------------"
echo -e "${YELLOW}To run E2E tests:${NC}"
echo -e "cd ${LOCAL_PATH} && npx playwright test email-attendees.test.ts --config=playwright.config.ts --reporter=list"

echo -e "${GREEN}===================================================${NC}"
echo -e "${GREEN}Email Attendees Test Suite Completed${NC}"