param(
    [int]$Port = 8080,
    [string]$HostName = "127.0.0.1"
)

$ErrorActionPreference = "Stop"

function Resolve-PhpCommand {
    $phpCommand = Get-Command php -ErrorAction SilentlyContinue
    if ($phpCommand) {
        return "php"
    }

    $candidates = @(
        "D:\\xampp\\php\\php.exe",
        "C:\\xampp\\php\\php.exe",
        "C:\\wamp64\\bin\\php\\php8.3.0\\php.exe",
        "C:\\wamp64\\bin\\php\\php8.2.0\\php.exe",
        "C:\\wamp64\\bin\\php\\php8.1.0\\php.exe"
    )

    foreach ($candidate in $candidates) {
        if (Test-Path $candidate) {
            return $candidate
        }
    }

    return $null
}

$php = Resolve-PhpCommand
if (-not $php) {
    Write-Error "PHP is not available in PATH and no common XAMPP/Wamp PHP executable was found. Install PHP or add php.exe to PATH."
}

if (-not (Test-Path "./mediawiki")) {
    Write-Error "Missing ./mediawiki. Run 'npm run mw:init' first."
}

if (-not (Test-Path "./mediawiki/index.php")) {
    Write-Error "./mediawiki does not look like a valid MediaWiki core directory."
}

Write-Host "Starting MediaWiki dev server at http://$HostName`:$Port"
Write-Host "Use Ctrl+C to stop."
& $php -S "$HostName`:$Port" -t "./mediawiki"
