# Enable URL Rewriting
RewriteEngine On

# Disable directory listing
Options -Indexes

# Set the base directory
RewriteBase /


# Redirect to HTTPS (uncomment in production)
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Block direct directory access under /assets (e.g., /assets/ or /assets/js/)
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} ^/assets/ [NC]
RewriteRule ^ - [R=404,L]

# Block direct access to template files
RewriteRule ^templates/ - [R=404,L]

# Route dynamic CSS through front controller (theme.php and custom-fonts.php generate CSS from DB)
RewriteRule ^assets/css/(theme|custom-fonts)\.php$ index.php?route=assets/css/$1.php [QSA,L]

# Redirect legacy API endpoints
RewriteRule ^(cart-api|search-api|set-currency|set-language)\.php$ /$1 [R=301,L]

# Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]

# Security headers
<IfModule mod_headers.c>
    Header unset Server
    Header always unset Server
    Header unset X-Powered-By
    Header always unset X-Powered-By
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-XSS-Protection "1; mode=block"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
    Header set Permissions-Policy "geolocation=(), microphone=(), camera=(), payment=(), usb=(), bluetooth=(), magnetometer=(), gyroscope=(), accelerometer=()"
    Header set X-Permitted-Cross-Domain-Policies "none"
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" env=HTTPS
</IfModule>

# Prevent access to sensitive files
<FilesMatch "\.(sqlite|db|log|ini|env)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>

# Block hidden files and placeholders
<FilesMatch "(^\.|\.gitkeep$)">
    Order Allow,Deny
    Deny from all
</FilesMatch>

# Enable compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/json
</IfModule>

# Cache static assets
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
</IfModule>

# PHP Settings for large file uploads (no limits for admin)
<IfModule mod_php.c>
    php_value upload_max_filesize 64M
    php_value post_max_size 70M
    php_value max_execution_time 120
    php_value max_input_time 120
    php_value memory_limit 256M
</IfModule>
