# EasyStream Docker Container URL Routing Configuration
# Optimized for Caddy + PHP-FPM container setup

RewriteEngine On

# Security Rules - Block access to sensitive directories
RewriteRule ^f_core/ - [F,L]
RewriteRule ^f_data/(?!media|uploads) - [F,L]
RewriteRule ^deploy/ - [F,L]
RewriteRule ^\.git/ - [F,L]
RewriteRule ^docker-compose\.yml$ - [F,L]
RewriteRule ^Dockerfile - [F,L]

# Container Health Check
RewriteRule ^health/?$ health_check.php [L]

# Token System Routes (Container-specific)
RewriteRule ^token[_-]purchase/?$ f_modules/m_frontend/m_donations/token_purchase.php [L,QSA]
RewriteRule ^token[_-]redemption/?$ f_modules/m_frontend/m_donations/token_redemption.php [L,QSA]
RewriteRule ^tokens/?$ f_modules/m_frontend/m_donations/token_purchase.php [L,QSA]

# Donation System Routes
RewriteRule ^donate/?$ f_modules/m_frontend/m_donations/rainforest_donation_form.php [L,QSA]
RewriteRule ^donation/?$ f_modules/m_frontend/m_donations/rainforest_donation_form.php [L,QSA]

# Main Application Routes - Route through index.php (which will use parser.php)
RewriteRule ^browse/?$ index.php [L,QSA]
RewriteRule ^videos/?$ index.php [L,QSA]
RewriteRule ^broadcasts/?$ index.php [L,QSA]
RewriteRule ^pictures/?$ index.php [L,QSA]
RewriteRule ^images/?$ index.php [L,QSA]
RewriteRule ^music/?$ index.php [L,QSA]
RewriteRule ^audios/?$ index.php [L,QSA]
RewriteRule ^documents/?$ index.php [L,QSA]
RewriteRule ^blogs/?$ index.php [L,QSA]
RewriteRule ^shorts/?$ index.php [L,QSA]
RewriteRule ^search/?$ index.php [L,QSA]
RewriteRule ^upload/?$ index.php [L,QSA]
RewriteRule ^view/([^/]+)/?$ index.php [L,QSA]

# User Account Routes - Route through index.php (which will use parser.php)
RewriteRule ^signin/?$ index.php [L,QSA]
RewriteRule ^signup/?$ index.php [L,QSA]
RewriteRule ^register/?$ index.php [L,QSA]
RewriteRule ^account/?$ index.php [L,QSA]
RewriteRule ^@([^/]+)/?$ index.php [L,QSA]
RewriteRule ^channel/([^/]+)/?$ index.php [L,QSA]

# Admin Panel Routes - Route to admin dashboard
RewriteRule ^admin/?$ admin_dashboard.php [L,QSA]
RewriteRule ^admin/([^/]+)/?$ admin_dashboard.php [L,QSA]
RewriteRule ^admin/([^/]+)/([^/]+)/?$ admin_dashboard.php [L,QSA]

# API Routes
RewriteRule ^api/(.*)$ f_modules/api/index.php?endpoint=$1 [L,QSA]

# Webhook Routes (Container-specific)
RewriteRule ^webhook/rainforest/?$ f_modules/m_frontend/m_donations/rainforest_webhook.php [L]
RewriteRule ^webhooks/(.*)$ f_modules/webhooks/$1.php [L]

# Container Media Routes (Volume mounts)
RewriteRule ^hls/(.*)$ /var/www/hls/$1 [L]
RewriteRule ^rec/(.*)$ /mnt/rec/$1 [L]
RewriteRule ^media/(.*)$ f_data/media/$1 [L]
RewriteRule ^uploads/(.*)$ f_data/uploads/$1 [L]
RewriteRule ^thumbs/(.*)$ f_data/thumbs/$1 [L]

# Static Asset Routes
RewriteRule ^css/(.*)$ f_templates/tpl_frontend/css/$1 [L]
RewriteRule ^js/(.*)$ f_scripts/fe/js/$1 [L]
RewriteRule ^img/(.*)$ f_scripts/fe/img/$1 [L]

# Legacy Support Routes
RewriteRule ^view/([^/]+)/?$ index.php?page=watch&v=$1 [L,QSA]
RewriteRule ^user/([^/]+)/?$ index.php?page=channel&user=$1 [L,QSA]

# Container-specific fallback - Route everything to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/f_modules/
RewriteCond %{REQUEST_URI} !^/admin_dashboard\.php
RewriteCond %{REQUEST_URI} !^/health_check\.php
RewriteCond %{REQUEST_URI} !^/f_data/
RewriteCond %{REQUEST_URI} !^/f_templates/
RewriteCond %{REQUEST_URI} !^/f_scripts/
RewriteRule ^(.*)$ index.php [L,QSA]

# Container PHP Configuration
php_flag display_errors Off
php_flag log_errors On
php_value max_execution_time 300
php_value memory_limit 512M
php_value upload_max_filesize 500M
php_value post_max_size 500M
