# ============================================
# HEARTITUDE API .HTACCESS
# ============================================

# Enable Rewrite Engine
RewriteEngine On

# ============================================
# PHP CONFIGURATION
# ============================================

# Set East African Time (EAT) - UTC+3
# Kenya, Tanzania, Uganda, Ethiopia, Somalia, etc.
<IfModule mod_php7.c>
    php_value date.timezone Africa/Nairobi
    php_value max_execution_time 120
    php_value max_input_time 120
    php_value memory_limit 256M
</IfModule>

<IfModule mod_php8.c>
    php_value date.timezone Africa/Nairobi
    php_value max_execution_time 120
    php_value max_input_time 120
    php_value memory_limit 256M
</IfModule>

# ============================================
# REWRITE RULES
# ============================================

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

# Redirect Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

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

# ============================================
# CORS HEADERS
# ============================================

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-Allow-Credentials "true"

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

# ============================================
# SECURITY HEADERS
# ============================================

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

# ============================================
# ADDITIONAL SECURITY
# ============================================

# Prevent directory listing
Options -Indexes

# Protect sensitive files
<FilesMatch "\.(env|ini|log|sh|sql|json|yml|yaml|lock|md)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Protect .htaccess itself
<Files .htaccess>
    Order allow,deny
    Deny from all
</Files>

# ============================================
# CACHING (Optional - Customize as needed)
# ============================================

# Cache static assets
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
    Header set Cache-Control "max-age=2592000, public"
</FilesMatch>

# ============================================
# ERROR LOGGING (with East African Time)
# ============================================

# Log errors with EAT timezone
php_value log_errors On
php_value error_log /home/ridcoltd/heartitudecloud.ridcoltd.co.ke/heartitude-api/logs/php_error.log

# Display errors off in production
php_value display_errors Off
php_value display_startup_errors Off

# For development, you can enable error display:
# php_value display_errors On
# php_value display_startup_errors On