# Cache Control Configuration
<IfModule mod_headers.c>
  # Cache busting for index.html - always check for updates
  <FilesMatch "index\.html$">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "0"
  </FilesMatch>
  
  # Cache assets with hash for 1 year (they have unique names when changed)
  <FilesMatch "\.(js|css)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>
  
  # Notas UF (/notas/) e Robo UF (/, D106): service worker e manifest sempre revalidados
  <FilesMatch "^(sw\.js|manifest\.webmanifest)$">
    Header set Cache-Control "no-cache"
  </FilesMatch>

  # Cache images and fonts for 1 month
  <FilesMatch "\.(ico|png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$">
    Header set Cache-Control "public, max-age=2592000"
  </FilesMatch>
  
  # Add ETag for better caching
  FileETag MTime Size
</IfModule>

# D106: manifest com o tipo certo (o host mandava application/octet-stream)
AddType application/manifest+json .webmanifest

# Gzip 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
</IfModule>

# Enable rewrite engine for Angular routing
<IfModule mod_rewrite.c>
  RewriteEngine On
  
  RewriteBase /

  # D36, plano B do link sem '#': /senha/<codigo> abre senha/index.html, que
  # redireciona para /#/senha/<codigo> (so vale se o Apache ler este .htaccess).
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^senha/[^/]+/?$ /senha/index.html [L]

  # Notas UF: app separado em /notas/ (index.html proprio, SW com escopo /notas/)
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^notas(/.*)?$ /notas/index.html [L]

  # Handle Angular routing - redirect all to index.html
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

# Security headers
<IfModule mod_headers.c>
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set X-Content-Type-Options "nosniff"
  Header always set X-XSS-Protection "1; mode=block"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule> 