# NGP Collection Dashboard - Apache Virtual Host Configuration (HTTP Only)
# Domain: dev.noesolution.com
# Path: /dashboard/

<VirtualHost *:80>
    ServerName dev.noesolution.com
    DocumentRoot /var/www/html/development/dashboard/frontend/build
    
    # Logging
    ErrorLog ${APACHE_LOG_DIR}/ngp-dashboard-error.log
    CustomLog ${APACHE_LOG_DIR}/ngp-dashboard-access.log combined
    LogLevel warn
    
    # Basic Security Headers
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # Enable compression
    <Location />
        SetOutputFilter DEFLATE
        SetEnvIfNoCase Request_URI \
            \.(?:gif|jpe?g|png|ico|woff|woff2|ttf|eot)$ no-gzip dont-vary
        SetEnvIfNoCase Request_URI \
            \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
    </Location>
    
    # Static file caching
    <LocationMatch "\.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$">
        ExpiresActive On
        ExpiresDefault "access plus 1 month"
        Header append Cache-Control "public, immutable"
    </LocationMatch>
    
    # Dashboard Frontend - Root access redirects to /dashboard/
    <Location "/">
        RewriteEngine On
        RewriteRule ^/?$ /dashboard/ [R=301,L]
    </Location>
    
    # Dashboard Frontend - Main application
    <Location "/dashboard">
        RewriteEngine On
        
        # Handle React Router (SPA)
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} ^/dashboard/
        RewriteRule . /dashboard/index.html [L]
        
        # CORS headers for API communication
        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"
        
        # Handle preflight requests
        RewriteCond %{REQUEST_METHOD} OPTIONS
        RewriteRule ^(.*)$ $1 [R=200,L]
    </Location>
    
    # API Proxy - Forward API requests to PHP backend
    <Location "/dashboard/api">
        # Remove /dashboard prefix and proxy to PHP API
        RewriteEngine On
        RewriteRule ^/dashboard/api/(.*)$ /development/dashboard/api/$1 [L]
        
        # API specific headers
        Header always set Content-Type "application/json"
        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"
    </Location>
    
    # Real-time WebSocket Proxy (if needed via HTTP)
    ProxyPreserveHost On
    ProxyRequests Off
    
    <Location "/dashboard/ws">
        ProxyPass "http://localhost:8080/"
        ProxyPassReverse "http://localhost:8080/"
        ProxyTimeout 300
        
        # WebSocket support
        RewriteEngine On
        RewriteCond %{HTTP:Upgrade} websocket [NC]
        RewriteCond %{HTTP:Connection} upgrade [NC]
        RewriteRule ^/dashboard/ws/?(.*) "ws://localhost:8080/$1" [P,L]
    </Location>
    
    # Block access to sensitive files
    <FilesMatch "\.(env|json|md|sql|log|pid|sh)$">
        Require all denied
    </FilesMatch>
    
    # Block access to node_modules and source files
    <LocationMatch "/(node_modules|src|\.git)/">
        Require all denied
    </LocationMatch>
    
    # Directory Options
    <Directory /var/www/html/development/dashboard/frontend/build>
        Options -Indexes +FollowSymLinks
        AllowOverride None
        Require all granted
        
        # Handle React Router
        RewriteEngine On
        RewriteBase /dashboard/
        RewriteRule ^index\.html$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /dashboard/index.html [L]
    </Directory>
    
    <Directory /var/www/html/development/dashboard/api>
        Options -Indexes
        AllowOverride None
        Require all granted
        
        # PHP configuration
        <FilesMatch "\.php$">
            SetHandler application/x-httpd-php
        </FilesMatch>
    </Directory>
</VirtualHost>

# Future HTTPS configuration (when SSL certificate is available)
# <VirtualHost *:443>
#     ServerName dev.noesolution.com
#     DocumentRoot /var/www/html/development/dashboard/frontend/build
#     
#     SSLEngine on
#     SSLCertificateFile /etc/ssl/certs/dev.noesolution.com.crt
#     SSLCertificateKeyFile /etc/ssl/private/dev.noesolution.com.key
#     SSLCertificateChainFile /etc/ssl/certs/ca-certificates.crt
#     
#     # Include same configuration as HTTP virtual host above
# </VirtualHost>