# ═══════════════════════════════════════════════════════════════
# Windows Localhost Disabler Script
# Run this in PowerShell as Administrator on your WINDOWS machine
# ═══════════════════════════════════════════════════════════════

Write-Host "═══════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host "Disabling Web Servers on Windows" -ForegroundColor Yellow
Write-Host "═══════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""

# Check if running as Administrator
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $isAdmin) {
    Write-Host "❌ ERROR: You must run PowerShell as Administrator!" -ForegroundColor Red
    Write-Host "Right-click PowerShell and select 'Run as Administrator'" -ForegroundColor Yellow
    pause
    exit
}

Write-Host "✅ Running as Administrator" -ForegroundColor Green
Write-Host ""

# Function to stop and disable service
function Stop-AndDisableService {
    param($serviceName)
    
    $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
    if ($service) {
        Write-Host "Found: $serviceName" -ForegroundColor Yellow
        if ($service.Status -eq 'Running') {
            Write-Host "  Stopping $serviceName..." -ForegroundColor Cyan
            Stop-Service -Name $serviceName -Force
            Write-Host "  ✅ Stopped" -ForegroundColor Green
        }
        Write-Host "  Disabling $serviceName..." -ForegroundColor Cyan
        Set-Service -Name $serviceName -StartupType Disabled
        Write-Host "  ✅ Disabled" -ForegroundColor Green
    } else {
        Write-Host "⚪ $serviceName not found (skip)" -ForegroundColor Gray
    }
    Write-Host ""
}

# Stop IIS
Write-Host "─────────────────────────────────────────────" -ForegroundColor Cyan
Write-Host "Checking IIS (Internet Information Services)" -ForegroundColor Yellow
Write-Host "─────────────────────────────────────────────" -ForegroundColor Cyan
Stop-AndDisableService "W3SVC"
Stop-AndDisableService "WAS"
Stop-AndDisableService "IISADMIN"

# Stop Apache
Write-Host "─────────────────────────────────────────────" -ForegroundColor Cyan
Write-Host "Checking Apache Web Server" -ForegroundColor Yellow
Write-Host "─────────────────────────────────────────────" -ForegroundColor Cyan
Stop-AndDisableService "Apache2.4"
Stop-AndDisableService "wampapache64"
Stop-AndDisableService "xamppApache"

# Check what's on port 80
Write-Host "─────────────────────────────────────────────" -ForegroundColor Cyan
Write-Host "Checking Port 80" -ForegroundColor Yellow
Write-Host "─────────────────────────────────────────────" -ForegroundColor Cyan

$port80 = Get-NetTCPConnection -LocalPort 80 -ErrorAction SilentlyContinue
if ($port80) {
    Write-Host "⚠️  Something is still using port 80:" -ForegroundColor Yellow
    foreach ($conn in $port80) {
        $process = Get-Process -Id $conn.OwningProcess -ErrorAction SilentlyContinue
        Write-Host "  PID: $($conn.OwningProcess) - $($process.Name)" -ForegroundColor Red
        
        $response = Read-Host "Kill this process? (Y/N)"
        if ($response -eq 'Y' -or $response -eq 'y') {
            Stop-Process -Id $conn.OwningProcess -Force
            Write-Host "  ✅ Killed process $($process.Name)" -ForegroundColor Green
        }
    }
} else {
    Write-Host "✅ Port 80 is FREE!" -ForegroundColor Green
}

Write-Host ""
Write-Host "═══════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host "✅ DONE!" -ForegroundColor Green
Write-Host "═══════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""
Write-Host "Now try in Firefox:" -ForegroundColor Yellow
Write-Host "  http://localhost:5000/" -ForegroundColor Cyan
Write-Host ""
Write-Host "Or:" -ForegroundColor Yellow
Write-Host "  http://192.168.10.130:5000/" -ForegroundColor Cyan
Write-Host ""

pause
