##############################################################################
#  LumenCache — Per-Site Nginx Configuration  (v1.0.2)
#  Light & Composition University
#
#  USE THIS FILE ONLY if you cannot modify the global server template
#  (e.g. shared hosting where you control individual vhost includes).
#  If you have applied nginx-global-template.conf to your server template,
#  you do NOT need this file.
#
#  Include it from your site's vhost:
#    include /var/www/vhosts/example.com/httpdocs/nginx-lumencache.conf;
#
#  IMPORTANT — map{} block
#  ────────────────────────
#  The $lumen_state map{} block must be placed in your nginx.conf http{}
#  context, NOT inside a server{} or location{} block.
#  Paste the following into your nginx.conf (or a conf.d/ include):
#
#    BUG FIX (v1.6.6): removed wp_woocommerce_session_ (set for EVERY visitor
#    on first load — bypassing on it defeats caching for all anonymous WC
#    traffic) and added memberpress_ + value-checked cart conditions to match
#    the live generated map (Server_Config::nginx_map_block()).
#
#    map $http_cookie $lumen_state {
#        default                           "";
#        "~*sys_active_state=true"         1;
#        "~*wordpress_logged_in_"          1;
#        "~*woocommerce_items_in_cart=[1-9]" 1;
#        "~*woocommerce_cart_hash=."       1;
#        "~*learndash_"                    1;
#        "~*llms_"                         1;
#        "~*mepr_"                         1;
#        "~*memberpress_"                  1;
#    }
##############################################################################


# ── 1. Primary location: try Redis first ─────────────────────────────────────

location / {
    # Use HTTP 418 as a clean internal signal to @fallback.
    error_page 418 = @fallback;

    # Bypass Redis for authenticated / active-session users.
    if ($lumen_state = 1) {
        return 418;
    }

    # Only cache GET requests.
    if ($request_method != GET) {
        return 418;
    }

    # Build the Redis key — must match the PHP plugin exactly.
    # Format: cache:{scheme}{method}{host}{request_uri}
    set $redis_key "cache:$scheme$request_method$host$request_uri";

    redis_pass   127.0.0.1:6379;
    default_type text/html;

    error_page 404 502 504 = @fallback;

    add_header X-Cache-Status "HIT";
    add_header X-Cache-Key    $redis_key;
}


# ── 2. Static assets — served directly from disk ─────────────────────────────

location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|otf|eot|mp4|webm|pdf|zip)$ {
    try_files     $uri =404;
    expires       max;
    log_not_found off;
    access_log    off;
    add_header    Cache-Control "public, immutable";
}


# ── 3. WordPress admin / API — always bypass Redis ───────────────────────────

location ~* ^/(wp-admin|wp-login\.php|wp-cron\.php|wp-json|xmlrpc\.php) {
    try_files $uri $uri/ /index.php?$args;
}


# ── 4. Cache miss fallback ────────────────────────────────────────────────────

location @fallback {
    try_files $uri $uri/ /index.php?$args;
    add_header X-Cache-Status "MISS";
}


# ── 5. PHP runtime ────────────────────────────────────────────────────────────
#
# Adjust the fastcgi_pass socket/address to match your PHP-FPM version.

location ~ \.php$ {
    try_files      $uri =404;
    fastcgi_pass   unix:/run/php/php8.3-fpm.sock;   # change to match your PHP version
    fastcgi_index  index.php;

    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

    # Production performance caps.
    fastcgi_connect_timeout  60s;
    fastcgi_send_timeout     120s;
    fastcgi_read_timeout     120s;
    fastcgi_buffers          16 16k;
    fastcgi_buffer_size      32k;

    # Cloudflare real-IP passthrough (harmless when not behind Cloudflare).
    fastcgi_param  HTTP_X_REAL_IP         $http_cf_connecting_ip;
    fastcgi_param  HTTP_X_FORWARDED_FOR   $proxy_add_x_forwarded_for;
    fastcgi_param  HTTP_X_FORWARDED_PROTO $scheme;
}
