# Entregadores App - Apache Configuration
RewriteEngine On

# Force HTTPS redirect
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Security Headers
<IfModule mod_headers.c>
    # PWA Security Headers
    Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "DENY"
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # PWA Specific Headers
    <FilesMatch "sw\.js$">
        Header set Cache-Control "no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "0"
        Header set Service-Worker-Allowed "/"
    </FilesMatch>
    
    <FilesMatch "manifest\.json$">
        Header set Content-Type "application/manifest+json"
        Header set Cache-Control "public, max-age=86400"
    </FilesMatch>
    
    # Static Assets Caching
    <FilesMatch "\.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
        Header set Vary "Accept-Encoding"
    </FilesMatch>
    
    # HTML Caching
    <FilesMatch "\.html$">
        Header set Cache-Control "public, max-age=3600"
    </FilesMatch>
    
    # API CORS Headers
    <FilesMatch "\.php$">
        Header set Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
        Header set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"
        Header set Access-Control-Max-Age "86400"
    </FilesMatch>
</IfModule>

# PWA File Handling
RewriteRule ^sw\.js$ /sw.js [L]
RewriteRule ^manifest\.json$ /manifest.json [L]

# Static Assets
RewriteRule ^static/ - [L]
RewriteRule ^backend/ - [L]
RewriteRule ^icon-.*\.png$ - [L]

# Specific file extensions
RewriteRule ^.*\.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|pdf|zip)$ - [L]

# API Routes (don't redirect to index.html)
RewriteCond %{REQUEST_URI} ^/backend/
RewriteRule ^(.*)$ - [L]

# Handle Client-Side Routing (React Router)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/backend/
RewriteCond %{REQUEST_URI} !^/static/
RewriteRule . /index.html [L]

# Compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/javascript
    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
    AddOutputFilterByType DEFLATE application/manifest+json
    
    # Don't compress images
    SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png|ico)$ no-gzip dont-vary
</IfModule>

# Browser Caching
<IfModule mod_expires.c>
    ExpiresActive On
    
    # PWA files
    ExpiresByType application/manifest+json "access plus 1 day"
    
    # Static assets
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType text/javascript "access plus 1 year"
    
    # Images
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/ico "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    
    # Fonts
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType application/font-woff2 "access plus 1 year"
    ExpiresByType application/font-woff "access plus 1 year"
    
    # HTML
    ExpiresByType text/html "access plus 1 hour"
    
    # Service Worker (no cache)
    <FilesMatch "sw\.js$">
        ExpiresDefault "access plus 0 seconds"
    </FilesMatch>
</IfModule>

# Error Pages
ErrorDocument 404 /index.html
ErrorDocument 403 /index.html
ErrorDocument 500 /index.html

# Security: Block access to sensitive files
<FilesMatch "(^#.*#|\.(bak|config|dist|fla|inc|ini|log|psd|sh|sql|sw[op])|~)$">
    Order allow,deny
    Deny from all
    Satisfy All
</FilesMatch>

# Prevent access to .htaccess itself
<Files ".htaccess">
    Order allow,deny
    Deny from all
</Files>

# Prevent access to .git directory
<DirectoryMatch "\.git">
    Order allow,deny
    Deny from all
</DirectoryMatch>

# Prevent access to node_modules if accidentally uploaded
<DirectoryMatch "node_modules">
    Order allow,deny
    Deny from all
</DirectoryMatch>

# Prevent access to source files
<FilesMatch "\.(tsx?|jsx?|map)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Allow only specific file types in root
<Files "*">
    Order deny,allow
    Deny from all
</Files>

<FilesMatch "\.(html|css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|pdf|txt|xml|json)$">
    Order allow,deny
    Allow from all
</FilesMatch>

# Performance: Enable ETags
FileETag MTime Size

# Performance: Remove Server Signature
ServerTokens Prod

# Performance: Enable Keep-Alive
<IfModule mod_headers.c>
    Header set Connection "Keep-Alive"
</IfModule>

# Security: Prevent MIME type sniffing
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
</IfModule>

# Security: Prevent clickjacking
<IfModule mod_headers.c>
    Header always set X-Frame-Options "DENY"
</IfModule>

# Security: Enable XSS protection
<IfModule mod_headers.c>
    Header always set X-XSS-Protection "1; mode=block"
</IfModule>

# Mobile Optimization
<IfModule mod_headers.c>
    # Optimize for mobile devices
    Header set Viewport "width=device-width, initial-scale=1.0"
    
    # Prevent mobile Safari from adjusting font sizes
    Header set -webkit-text-size-adjust "100%"
    Header set -ms-text-size-adjust "100%"
</IfModule>

# PWA Installation
<IfModule mod_headers.c>
    # Support PWA installation
    <FilesMatch "\.html$">
        Header set Link "</manifest.json>; rel=manifest"
    </FilesMatch>
</IfModule>