# Enable URL rewriting
RewriteEngine On

# Set the base directory for the application
RewriteBase /HomeHardware_PWA/HomeHardware/htdocs/

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Set MIME types for PWA related files
AddType application/manifest+json .webmanifest
AddType application/x-web-app-manifest+json .webapp
AddType text/cache-manifest .appcache

# Enable CORS for service worker and assets
<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
    Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization"
    
    # Set CORS for service worker
    <FilesMatch "\.(js|css|json|xml|woff|woff2|ttf|eot|svg|png|jpg|jpeg|gif|ico|webmanifest)$">
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>
    
    # Security headers
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-XSS-Protection "1; mode=block"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # PWA related headers
    Header set Service-Worker-Allowed "/"
    Header set X-Content-Type-Options "nosniff"
</IfModule>

# Enable compression for faster page loads
<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>

# Set cache control for 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 text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 2 days"
</IfModule>

# Disable directory browsing
Options -Indexes

# Prevent viewing of .htaccess file
<Files .htaccess>
    Order allow,deny
    Deny from all
</Files>

# Prevent access to sensitive files
<FilesMatch "^(composer\.json|composer\.lock|package\.json|package-lock\.json|webpack\.config\.js|gulpfile\.js|yarn\.lock|phpunit\.xml|\.env|README\.md|CHANGELOG\.md|LICENSE|CONTRIBUTING\.md|UPGRADING\.md)">
    Order allow,deny
    Deny from all
</FilesMatch>

# Custom error documents
ErrorDocument 404 /HomeHardware_PWA/HomeHardware/htdocs/404.html
ErrorDocument 500 /HomeHardware_PWA/HomeHardware/htdocs/500.html
