# NGP Collection Dashboard - Apache VirtualHost Configuration
# File: /etc/apache2/sites-available/ngp-dashboard.conf

<VirtualHost *:80>
    ServerName dev.noesolution.com
    DocumentRoot /var/www/html/development/dashboard/frontend/build
    
    # Dashboard Frontend
    Alias /dashboard /var/www/html/development/dashboard/frontend/build
    <Directory "/var/www/html/development/dashboard/frontend/build">
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
        
        # React Router support
        RewriteEngine On
        RewriteBase /dashboard/
        RewriteRule ^index\.html$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /dashboard/index.html [L]
    </Directory>
    
    # Dashboard API
    Alias /dashboard/api /var/www/html/development/dashboard/api
    <Directory "/var/www/html/development/dashboard/api">
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
        
        # PHP API routing
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        
        # API Routes
        RewriteRule ^auth/(.*)$ auth.php [QSA,L]
        RewriteRule ^dashboard/(.*)$ dashboard.php [QSA,L]
        
        # 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"
        Header always set Access-Control-Max-Age "3600"
        
        # Handle preflight requests
        RewriteCond %{REQUEST_METHOD} OPTIONS
        RewriteRule ^(.*)$ $1 [R=200,L]
    </Directory>
    
    # 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"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # 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>
    
    # Caching for static assets
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresByType text/css "access plus 1 month"
        ExpiresByType application/javascript "access plus 1 month"
        ExpiresByType image/png "access plus 1 month"
        ExpiresByType image/jpg "access plus 1 month"
        ExpiresByType image/jpeg "access plus 1 month"
        ExpiresByType image/gif "access plus 1 month"
        ExpiresByType image/svg+xml "access plus 1 month"
    </IfModule>
    
    # Logging
    ErrorLog ${APACHE_LOG_DIR}/ngp-dashboard-error.log
    CustomLog ${APACHE_LOG_DIR}/ngp-dashboard-access.log combined
</VirtualHost>

# SSL Configuration (if SSL certificate is available)
<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName dev.noesolution.com
    DocumentRoot /var/www/html/development/dashboard/frontend/build
    
    # SSL Configuration
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/dev.noesolution.com.crt
    SSLCertificateKeyFile /etc/ssl/private/dev.noesolution.com.key
    
    # Same configuration as HTTP version
    Alias /dashboard /var/www/html/development/dashboard/frontend/build
    <Directory "/var/www/html/development/dashboard/frontend/build">
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
        
        RewriteEngine On
        RewriteBase /dashboard/
        RewriteRule ^index\.html$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /dashboard/index.html [L]
    </Directory>
    
    Alias /dashboard/api /var/www/html/development/dashboard/api
    <Directory "/var/www/html/development/dashboard/api">
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
        
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        
        RewriteRule ^auth/(.*)$ auth.php [QSA,L]
        RewriteRule ^dashboard/(.*)$ dashboard.php [QSA,L]
        
        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"
        Header always set Access-Control-Max-Age "3600"
        
        RewriteCond %{REQUEST_METHOD} OPTIONS
        RewriteRule ^(.*)$ $1 [R=200,L]
    </Directory>
    
    # 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"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
    
    # Same compression and caching as HTTP
    <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>
    
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresByType text/css "access plus 1 month"
        ExpiresByType application/javascript "access plus 1 month"
        ExpiresByType image/png "access plus 1 month"
        ExpiresByType image/jpg "access plus 1 month"
        ExpiresByType image/jpeg "access plus 1 month"
        ExpiresByType image/gif "access plus 1 month"
        ExpiresByType image/svg+xml "access plus 1 month"
    </IfModule>
    
    ErrorLog ${APACHE_LOG_DIR}/ngp-dashboard-ssl-error.log
    CustomLog ${APACHE_LOG_DIR}/ngp-dashboard-ssl-access.log combined
</VirtualHost>
</IfModule>