# ============================================================
# DBAOps Futuristic Enterprise™ - IIS Setup
# ============================================================
# Canonical path: M:\RHOYLLE INNOVATION EMPIRE LLC\DBAOps Futuristic Enterprise
param([string]$WebRoot = "M:\RHOYLLE INNOVATION EMPIRE LLC\DBAOps Futuristic Enterprise\WebDashboard")

Import-Module WebAdministration

# Create App Pool
if (-not (Test-Path "IIS:\AppPools\DBAOpsAppPool")) {
    New-WebAppPool -Name "DBAOpsAppPool"
    Set-ItemProperty "IIS:\AppPools\DBAOpsAppPool" -Name "managedRuntimeVersion" -Value ""
    Set-ItemProperty "IIS:\AppPools\DBAOpsAppPool" -Name "processModel.identityType" -Value "NetworkService"
    Write-Host "[OK] App Pool created" -ForegroundColor Green
}

# Create Website
if (-not (Get-Website -Name "DBAOps" -EA SilentlyContinue)) {
    New-Website -Name "DBAOps" -PhysicalPath $WebRoot -ApplicationPool "DBAOpsAppPool" -Port 8080
    Write-Host "[OK] Website created on port 8080" -ForegroundColor Green
}

# Enable Windows Authentication
Set-WebConfigurationProperty -Filter "/system.webServer/security/authentication/windowsAuthentication" -Name "enabled" -Value $true -PSPath "IIS:\Sites\DBAOps"
Set-WebConfigurationProperty -Filter "/system.webServer/security/authentication/anonymousAuthentication" -Name "enabled" -Value $false -PSPath "IIS:\Sites\DBAOps"

# Add HTTPS binding if cert exists
$cert = Get-ChildItem Cert:\LocalMachine\My | Where-Object { $_.Subject -like "*dbaops*" } | Select-Object -First 1
if ($cert) {
    New-WebBinding -Name "DBAOps" -Protocol "https" -Port 443 -HostHeader "dbaopsdash"
    $binding = Get-WebBinding -Name "DBAOps" -Protocol "https"
    $binding.AddSslCertificate($cert.Thumbprint, "My")
    Write-Host "[OK] HTTPS binding added" -ForegroundColor Green
}

# Set JSON MIME type
Add-WebConfigurationProperty -PSPath "IIS:\Sites\DBAOps" -Filter "system.webServer/staticContent" -Name "." -Value @{fileExtension='.json';mimeType='application/json'} -EA SilentlyContinue

# Start
Start-Website -Name "DBAOps"
Write-Host "[OK] DBAOps website started" -ForegroundColor Green