RewriteEngine On

# Handle CORS preflight requests
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]

# Redirect old API files to new structure
RewriteRule ^register\.php$ /api/register.php [L]
RewriteRule ^get_login\.php$ /api/get_login.php [L]
RewriteRule ^get_user\.php$ /api/get_user.php [L]
RewriteRule ^get_user_profile\.php$ /api/get_user_profile.php [L]
RewriteRule ^update_profile\.php$ /api/update_profile.php [L]
RewriteRule ^update_password\.php$ /api/update_password.php [L]
RewriteRule ^upload_foto_profil\.php$ /api/upload_foto_profil.php [L]
RewriteRule ^get_check_phone\.php$ /api/get_check_phone.php [L]
RewriteRule ^get_otp_wa\.php$ /api/get_otp_wa.php [L]
RewriteRule ^get_login_otp\.php$ /api/get_login_otp.php [L]
RewriteRule ^send_registration_otp\.php$ /api/send_registration_otp.php [L]
RewriteRule ^verify_registration_otp\.php$ /api/verify_registration_otp.php [L]
RewriteRule ^verify_otp\.php$ /api/verify_otp.php [L]
RewriteRule ^banner_detail\.php$ /api/banner_detail.php [L]
RewriteRule ^banner\.php$ /api/banner.php [L]
RewriteRule ^save_banner_click\.php$ /api/save_banner_click.php [L]
RewriteRule ^news\.php$ /api/news.php [L]
RewriteRule ^news_detail\.php$ /api/news_detail.php [L]
RewriteRule ^save_fcm_token\.php$ /api/save_fcm_token.php [L]
RewriteRule ^update_fcm_token\.php$ /api/update_fcm_token.php [L]

# API versioning support
RewriteRule ^v2/(.*)$ /api/$1 [L]

# Default to API index if no specific file requested
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ /api/index.php [L]

# Security headers
Header always set X-Content-Type-Options nosniff
Header always set X-Frame-Options DENY
Header always set X-XSS-Protection "1; mode=block"

# CORS headers
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"
Header always set Access-Control-Max-Age "3600"

# Prevent access to sensitive files
<FilesMatch "\.(log|txt|md|sql|json)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Prevent access to configuration files
<Files "config.php">
    Order allow,deny
    Deny from all
</Files>

# 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>

# Set cache headers for static files
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/json "access plus 1 hour"
</IfModule>