#!/bin/bash

# Script to disable Breeze cache for testing environments
# This creates an mu-plugin that sets DONOTCACHEPAGE constant

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

# Source environment variables
if [ -f "$PROJECT_ROOT/.env" ]; then
    source "$PROJECT_ROOT/.env"
fi

# Check for required environment variables
if [ -z "$UPSKILL_STAGING_SSH_USER" ] || [ -z "$UPSKILL_STAGING_PASS" ] || [ -z "$UPSKILL_STAGING_IP" ] || [ -z "$UPSKILL_STAGING_PATH" ]; then
    echo "Error: Missing required environment variables."
    echo "Please ensure the following are set in your .env file:"
    echo "  - UPSKILL_STAGING_SSH_USER"
    echo "  - UPSKILL_STAGING_PASS"
    echo "  - UPSKILL_STAGING_IP"
    echo "  - UPSKILL_STAGING_PATH"
    exit 1
fi

echo "Creating mu-plugin to disable Breeze cache for testing..."

# Create the mu-plugin content
MU_PLUGIN_CONTENT='<?php
/**
 * Disable Breeze cache for testing environments
 */

// Disable Breeze cache when running tests
if (
    getenv("WP_ENV") === "testing" ||
    defined("WP_TESTS_DOMAIN") ||
    strpos($_SERVER["HTTP_USER_AGENT"] ?? "", "Playwright") !== false ||
    isset($_GET["no_cache_test"]) ||
    (isset($_SERVER["REQUEST_URI"]) && strpos($_SERVER["REQUEST_URI"], "/manage-event/") !== false)
) {
    if (!defined("DONOTCACHEPAGE")) {
        define("DONOTCACHEPAGE", true);
    }
}

// Additional Breeze filter to ensure cache is disabled
add_filter("breeze_override_donotcachepage", function($do_not_cache) {
    if (
        getenv("WP_ENV") === "testing" ||
        defined("WP_TESTS_DOMAIN") ||
        strpos($_SERVER["HTTP_USER_AGENT"] ?? "", "Playwright") !== false ||
        isset($_GET["no_cache_test"])
    ) {
        return false; // This returns false to the override, which means DO cache page = false
    }
    return $do_not_cache;
});
'

# Create mu-plugin via SSH
echo "Creating mu-plugin on staging server..."
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"cd ${UPSKILL_STAGING_PATH} && \
mkdir -p wp-content/mu-plugins && \
cat > wp-content/mu-plugins/disable-breeze-for-tests.php << 'EOF'
$MU_PLUGIN_CONTENT
EOF"

# Verify the file was created
echo "Verifying mu-plugin creation..."
FILE_EXISTS=$(sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"cd ${UPSKILL_STAGING_PATH} && [ -f wp-content/mu-plugins/disable-breeze-for-tests.php ] && echo 'exists' || echo 'not found'")

if [ "$FILE_EXISTS" = "exists" ]; then
    echo "✅ Successfully created mu-plugin to disable Breeze cache for testing"
    echo "The following conditions will disable cache:"
    echo "  - WP_ENV environment variable is set to 'testing'"
    echo "  - WP_TESTS_DOMAIN constant is defined"
    echo "  - User agent contains 'Playwright'"
    echo "  - URL has 'no_cache_test' query parameter"
    echo "  - URL contains '/manage-event/'"
else
    echo "❌ Failed to create mu-plugin"
    exit 1
fi

# Clear existing cache
echo "Clearing existing Breeze cache..."
$SCRIPT_DIR/clear-breeze-cache.sh

echo "✅ Breeze cache setup for testing complete"