$erpVhostPath = "C:\laragon\etc\apache2\sites-enabled\erp.almanara.serveirc.com.conf"

# Update VHost file to enable SSL on port 8333
# We also add ServerAlias almanara.serveirc.com to the 8333 block so accessing the main domain on 8333 works with SSL.
$vhostContent = @"
<VirtualHost *:8333>
    ServerName erp.almanara.serveirc.com
    ServerAlias almanara.serveirc.com
    
    SSLEngine on
    SSLCertificateFile "C:/laragon/etc/ssl/almanara_serveirc_com.pem"
    SSLCertificateKeyFile "C:/laragon/etc/ssl/private.key"
    
    ProxyPreserveHost On
    RequestHeader set X-Forwarded-Proto https
    ProxyPass / http://127.0.0.1:8091/
    ProxyPassReverse / http://127.0.0.1:8091/
</VirtualHost>

<VirtualHost *:443>
    ServerName erp.almanara.serveirc.com
    
    SSLEngine on
    SSLCertificateFile "C:/laragon/etc/ssl/almanara_serveirc_com.pem"
    SSLCertificateKeyFile "C:/laragon/etc/ssl/private.key"
    
    ProxyPreserveHost On
    RequestHeader set X-Forwarded-Proto https
    ProxyPass / http://127.0.0.1:8091/
    ProxyPassReverse / http://127.0.0.1:8091/
</VirtualHost>
"@

try {
    Set-Content -Path $erpVhostPath -Value $vhostContent -Encoding UTF8
    Write-Host "Updated $erpVhostPath to enforce SSL on port 8333"
} catch {
    Write-Error "Failed to update vhost file: $_"
}

# Restart Apache
try {
    & "C:\laragon\bin\apache\httpd-2.4.54-win64-VS16\bin\httpd.exe" -k restart
    Write-Host "Apache restarted"
} catch {
    Write-Error "Failed to restart Apache: $_"
}
