# ============================================================
# Performance optimizations — no functional changes
# ============================================================

# Enable Gzip compression for text-based assets.
# (Brotli is preferred when supported by the server, but gzip works
# universally and most shared hosts ship mod_deflate.)
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
    AddOutputFilterByType DEFLATE application/javascript application/json application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE font/ttf font/otf font/eot
</IfModule>

# Aggressive caching for static assets.
# Filenames include a ?v=<mtime> query string (header.php style) so when a
# file actually changes the URL changes too — safe to cache for a year.
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault                          "access plus 1 month"

    # HTML — keep short so changes are seen quickly
    ExpiresByType text/html                 "access plus 1 hour"

    # CSS / JS — cache hard
    ExpiresByType text/css                  "access plus 1 year"
    ExpiresByType application/javascript    "access plus 1 year"
    ExpiresByType text/javascript           "access plus 1 year"
    ExpiresByType application/json          "access plus 1 hour"

    # Images — long cache; product images are addressed by filename
    ExpiresByType image/png                 "access plus 1 year"
    ExpiresByType image/jpeg                "access plus 1 year"
    ExpiresByType image/jpg                 "access plus 1 year"
    ExpiresByType image/webp                "access plus 1 year"
    ExpiresByType image/svg+xml             "access plus 1 year"
    ExpiresByType image/x-icon              "access plus 1 year"
    ExpiresByType image/gif                 "access plus 1 year"

    # Fonts
    ExpiresByType font/ttf                  "access plus 1 year"
    ExpiresByType font/otf                  "access plus 1 year"
    ExpiresByType font/woff                 "access plus 1 year"
    ExpiresByType font/woff2                "access plus 1 year"
    ExpiresByType application/font-woff     "access plus 1 year"
    ExpiresByType application/font-woff2    "access plus 1 year"

    # Media
    ExpiresByType video/mp4                 "access plus 1 month"
</IfModule>

# Add Cache-Control headers (in case mod_expires isn't loaded).
# Honors any ?v=<mtime> query-string cache-busting used in PHP templates.
<IfModule mod_headers.c>
    <FilesMatch "\.(css|js|jpg|jpeg|png|gif|webp|svg|ico|woff2?)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>

    <FilesMatch "\.(html|htm)$">
        Header set Cache-Control "public, max-age=3600, must-revalidate"
    </FilesMatch>

    # Allow the fonts.googleapis.com CORS preflight to be cached.
    <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2)$">
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>

    # Disable ETag for static files served through mod_rewrite / PHP — modern
    # Cache-Control: immutable is more efficient. Keep ETags for HTML.
    <FilesMatch "\.(css|js|jpg|jpeg|png|gif|webp|svg|ico|woff2?)$">
        Header unset ETag
    </FilesMatch>
    FileETag MTime Size

    # Vary on Accept-Encoding so compressed + uncompressed variants are
    # stored separately by caches/proxies.
    <FilesMatch "\.(css|js|html|htm)$">
        Header append Vary Accept-Encoding
    </FilesMatch>

    # X-Content-Type-Options: keep the configured MIME types trustworthy.
    Header set X-Content-Type-Options "nosniff"
</IfModule>

# Disable directory listings (security + perf — one less round trip / probe).
Options -Indexes

# Keep-alive on for browser-side connection reuse (default is on, this just
# makes it explicit if a host has overridden it).
<IfModule mod_headers.c>
    Header set Connection "keep-alive"
</IfModule>

# Cache-busting friendly: pass through existing ?v= query strings used in
# templates (header.php uses ?v=filemtime(...)). No rewrite needed; this is
# here to document intent.