# Guarulhos Liquor Store - Apache Configuration
# This .htaccess file handles URL rewriting and server configuration

# ============================================
# 1. Enable mod_rewrite
# ============================================
<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Set the base directory for rewrites (domain-agnostic)
    RewriteBase /Guarulhos%20Liquor%20Stores/
    
    # Remove .php extension from URLs (optional - only if you want clean URLs)
    # RewriteCond %{REQUEST_FILENAME} !-f
    # RewriteCond %{REQUEST_FILENAME} !-d
    # RewriteRule ^([a-zA-Z0-9_-]+)$ $1.php [L]
    
    # Prevent direct access to PHP files (optional security)
    # RewriteRule ^(callback|webhook)\.php$ - [F]
</IfModule>

# ============================================
# 2. Enable Gzip Compression
# ============================================
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/json
</IfModule>

# ============================================
# 3. Caching Headers (Improve Performance)
# ============================================
<IfModule mod_expires.c>
    ExpiresEngine On
    ExpiresDefault "access plus 1 week"
    
    # Cache images for 1 month
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/webp "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"
    
    # Cache CSS & JavaScript for 1 week
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType text/javascript "access plus 1 week"
    
    # Cache fonts for 1 month
    ExpiresByType font/truetype "access plus 1 month"
    ExpiresByType font/opentype "access plus 1 month"
    ExpiresByType application/x-font-woff "access plus 1 month"
    
    # Don't cache HTML (always fresh)
    ExpiresByType text/html "access plus 0 seconds"
</IfModule>

# ============================================
# 4. Security Headers
# ============================================
<IfModule mod_headers.c>
    # Prevent MIME type sniffing
    Header always set X-Content-Type-Options "nosniff"
    
    # Enable XSS protection
    Header always set X-XSS-Protection "1; mode=block"
    
    # Prevent clickjacking
    Header always set X-Frame-Options "SAMEORIGIN"
    
    # Referrer Policy
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# ============================================
# 5. Disable Directory Listing
# ============================================
Options -Indexes

# ============================================
# 6. Prevent Access to Sensitive Files
# ============================================
<FilesMatch "\.(env|config|php.ini|log)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Allow access to .htaccess (read-only)
<Files .htaccess>
    Order allow,deny
    Allow from all
</Files>

# ============================================
# 7. Set PHP Configuration (if needed)
# ============================================
<IfModule mod_php.c>
    php_flag display_errors Off
    php_flag log_errors On
    php_value upload_max_filesize 50M
    php_value post_max_size 50M
    php_value max_execution_time 300
</IfModule>

# ============================================
# 8. Handle 404 Errors (serve index.html for SPA routing)
# ============================================
<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.html [L]
</IfModule>

# ============================================
# 9. UTF-8 Encoding
# ============================================
AddCharset UTF-8 .html .css .js .json .xml

# ============================================
# 10. Proper MIME Types
# ============================================
<IfModule mod_mime.c>
    AddType application/json .json
    AddType application/javascript .js
    AddType text/css .css
</IfModule>
