# Pilot reverse proxy rules for routing selected Magento REST traffic to the
# Cloudflare continuity worker while keeping all other traffic on Magento.
#
# Usage:
# - include these location blocks inside the existing TLS vhost serving the
#   Magento domain that FarApp/Plytix currently calls
# - keep your current generic PHP/Magento locations after these more specific
#   routes

proxy_ssl_server_name on;

# Shared upstream for the continuity worker.
set $hsworker1_upstream https://hsworker1.piesol.com;

# Common proxy headers for Worker-bound integration traffic.
proxy_http_version 1.1;
proxy_set_header Host hsworker1.piesol.com;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Original-URI $request_uri;
proxy_set_header X-Original-Method $request_method;
proxy_set_header Connection "";

# Large product bodies and slower upstream processing need relaxed timeouts.
client_max_body_size 25m;
proxy_connect_timeout 15s;
proxy_send_timeout 120s;
proxy_read_timeout 120s;

# 1. Admin token requests
location = /index.php/rest/V1/integration/admin/token {
    proxy_pass $hsworker1_upstream;
}

# 2. Product upserts
location ~ ^/index\.php/rest/all/V1/products/[^/]+$ {
    proxy_pass $hsworker1_upstream;
}

# 3. Inventory source updates
location = /index.php/rest/all/V1/inventory/source-items {
    proxy_pass $hsworker1_upstream;
}

# 4. Lightweight read endpoints that the Worker can cache safely
location = /index.php/rest/V1/modules {
    proxy_pass $hsworker1_upstream;
}

location = /index.php/rest/V1/store/storeViews {
    proxy_pass $hsworker1_upstream;
}

location = /index.php/rest/V1/store/websites {
    proxy_pass $hsworker1_upstream;
}

# Optional non-index.php variants if a caller uses clean Magento REST paths.
location = /rest/V1/integration/admin/token {
    proxy_pass $hsworker1_upstream;
}

location ~ ^/rest/all/V1/products/[^/]+$ {
    proxy_pass $hsworker1_upstream;
}

location = /rest/all/V1/inventory/source-items {
    proxy_pass $hsworker1_upstream;
}

location = /rest/V1/modules {
    proxy_pass $hsworker1_upstream;
}

location = /rest/V1/store/storeViews {
    proxy_pass $hsworker1_upstream;
}

location = /rest/V1/store/websites {
    proxy_pass $hsworker1_upstream;
}
