#!/usr/bin/env bash
#
# Runs the cache-busting test suite once per supported page cache plugin.
# For each plugin: install, activate, enable page caching, warm the cache,
# verify the paywalled post is NOT cached and the unpaywalled control IS,
# then run the full header-assertion suite and tear down.
#
# Usage: bash tests/test-with-cache-plugin.sh
#
# WP Rocket and LiteSpeed Cache are paid / require specific server software
# and are excluded from this matrix - verify them manually.

set -u
DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck disable=SC1091
source "$DIR/lib.sh"

PLUGINS=(wp-super-cache w3-total-cache cache-enabler wp-fastest-cache)

OVERALL_FAILED=0

enable_caching() {
  local slug="$1"
  case "$slug" in
    wp-super-cache)
      wpcli super-cache enable > /dev/null || true
      ;;
    w3-total-cache)
      wpcli w3-total-cache option set pgcache.enabled true --type=boolean > /dev/null || true
      wpcli w3-total-cache flush all > /dev/null || true
      ;;
    cache-enabler)
      wpcli option patch update cache_enabler permalink_structure '""' --format=json > /dev/null || true
      ;;
    wp-fastest-cache)
      # WP Fastest Cache uses option key WpFastestCacheStatus; just activate is enough for the cache to write
      wpcli option update WpFastestCacheStatus on > /dev/null || true
      ;;
  esac
}

cache_dir_for() {
  local slug="$1"
  case "$slug" in
    wp-super-cache)   echo "$WP_PATH/wp-content/cache/supercache" ;;
    w3-total-cache)   echo "$WP_PATH/wp-content/cache/page_enhanced" ;;
    cache-enabler)    echo "$WP_PATH/wp-content/cache/cache-enabler" ;;
    wp-fastest-cache) echo "$WP_PATH/wp-content/cache/all" ;;
  esac
}

# Run the full test once with no extra cache plugin first (baseline)
info "===== Baseline (no cache plugin) ====="
bash "$DIR/test-cache-busting.sh" || OVERALL_FAILED=1

for slug in "${PLUGINS[@]}"; do
  echo
  info "===== With cache plugin: $slug ====="

  wpcli plugin install "$slug" --activate > /dev/null || {
    fail "$slug: install/activate failed; skipping"
    continue
  }
  enable_caching "$slug"

  # Warm cache: hit unpaywalled control + paywalled post twice
  POST_A_URL=$(post_permalink "$(cat "$POST_A_FILE" 2>/dev/null || echo 0)" 2>/dev/null || echo "")
  POST_B_URL=$(post_permalink "$(cat "$POST_B_FILE" 2>/dev/null || echo 0)" 2>/dev/null || echo "")
  if [ -z "$POST_A_URL" ] || [ -z "$POST_B_URL" ]; then
    # The previous run tore down its posts; we need to set up state again for the warm-cache check
    setup_state
    POST_A_URL=$(post_permalink "$(cat "$POST_A_FILE")")
    POST_B_URL=$(post_permalink "$(cat "$POST_B_FILE")")
  fi

  curl -s "$POST_A_URL" > /dev/null
  curl -s "$POST_A_URL" > /dev/null
  curl -s "$POST_B_URL" > /dev/null
  curl -s "$POST_B_URL" > /dev/null

  CACHE_DIR=$(cache_dir_for "$slug")
  if [ -d "$CACHE_DIR" ]; then
    A_SLUG=$(basename "$POST_A_URL")
    B_SLUG=$(basename "$POST_B_URL")
    if find "$CACHE_DIR" -path "*$A_SLUG*" 2>/dev/null | grep -q .; then
      pass "$slug: unpaywalled Post A IS in cache directory"
    else
      info "  ($slug: Post A not found in $CACHE_DIR - cache may use a different layout; not a hard failure)"
    fi
    if find "$CACHE_DIR" -path "*$B_SLUG*" 2>/dev/null | grep -q .; then
      fail "$slug: paywalled Post B was cached - cache-busting did not work"
    else
      pass "$slug: paywalled Post B is NOT in cache directory"
    fi
  else
    info "  ($slug: cache directory $CACHE_DIR not found - plugin may use a different path)"
  fi

  # Now run the full header-assertion suite with the cache plugin active
  bash "$DIR/test-cache-busting.sh" || OVERALL_FAILED=1

  # Deactivate
  wpcli plugin deactivate "$slug" --uninstall > /dev/null || true
  rm -rf "$CACHE_DIR" 2>/dev/null || true
done

echo
if [ "$OVERALL_FAILED" -eq 0 ]; then
  echo "${C_GREEN}All cache-plugin scenarios passed.${C_RESET}"
else
  echo "${C_RED}One or more cache-plugin scenarios failed.${C_RESET}"
fi
exit "$OVERALL_FAILED"
