$httpdPath = "C:\laragon\bin\apache\httpd-2.4.54-win64-VS16\conf\httpd.conf"
$erpVhostPath = "C:\laragon\etc\apache2\sites-enabled\erp.almanara.serveirc.com.conf"

# 1. Fix Listen directive in httpd.conf
try {
    $httpdContent = Get-Content -Path $httpdPath -Raw
    $modified = $false
    
    # Check if Listen 8333 exists
    if ($httpdContent -notmatch "(?m)^Listen 8333") {
        # If Listen 8090 exists, replace it with Listen 8333
        if ($httpdContent -match "(?m)^Listen 8090") {
            $httpdContent = $httpdContent -replace "(?m)^Listen 8090", "Listen 8333"
            $modified = $true
            Write-Host "Replaced Listen 8090 with Listen 8333"
        } else {
            # Just insert Listen 8333 after the commented out example
            $httpdContent = $httpdContent -replace "#Listen 12.34.56.78:8333", "#Listen 12.34.56.78:8333`r`nListen 8333"
            $modified = $true
            Write-Host "Added Listen 8333"
        }
    } elseif ($httpdContent -match "(?m)^Listen 8090") {
        # If both exist, remove 8090
        $httpdContent = $httpdContent -replace "(?m)^Listen 8090\s*(\r\n|\n)?", ""
        $modified = $true
        Write-Host "Removed extra Listen 8090"
    }
    
    # Ensure proxy modules are enabled (Laragon might have them disabled by default, but we need them for ERPNext)
    if ($httpdContent -match "#LoadModule proxy_module") {
        $httpdContent = $httpdContent -replace "#LoadModule proxy_module", "LoadModule proxy_module"
        $modified = $true
        Write-Host "Enabled proxy_module"
    }
    if ($httpdContent -match "#LoadModule proxy_http_module") {
        $httpdContent = $httpdContent -replace "#LoadModule proxy_http_module", "LoadModule proxy_http_module"
        $modified = $true
        Write-Host "Enabled proxy_http_module"
    }
    
    if ($modified) {
        Set-Content -Path $httpdPath -Value $httpdContent -Encoding UTF8
        Write-Host "Updated httpd.conf"
    } else {
        Write-Host "httpd.conf already has correct Listen 8333 and proxy modules."
    }
} catch {
    Write-Error "Failed to update httpd.conf: $_"
}

# 2. Verify/Create VHost file for ERPNext Subdomain
$vhostContent = @"
<VirtualHost *:8333>
    ServerName erp.almanara.serveirc.com
    ProxyPreserveHost On
    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 {
    # Always overwrite to ensure correctness
    Set-Content -Path $erpVhostPath -Value $vhostContent -Encoding UTF8
    Write-Host "Ensured $erpVhostPath content"
} catch {
    Write-Error "Failed to update vhost file: $_"
}

# 3. 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: $_"
}
