Param(
    [Parameter(ValueFromRemainingArguments = $true)]
    [string[]]$PhpUnitArgs
)

$ErrorActionPreference = "Stop"

function Resolve-PhpExe {
    $cmd = Get-Command php -ErrorAction SilentlyContinue
    if ($cmd -and $cmd.Source) {
        return $cmd.Source
    }

    $candidates = @(
        "C:\xampp\php\php.exe",
        "C:\php\php.exe",
        "C:\Program Files\PHP\php.exe"
    )

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

    throw "Nie znaleziono interpretera PHP. Dodaj php do PATH albo zainstaluj PHP (np. XAMPP)."
}

$phpExe = Resolve-PhpExe
$phpUnitPhar = Join-Path $PSScriptRoot "phpunit.phar"

if (-not (Test-Path $phpUnitPhar)) {
    throw "Brak pliku phpunit.phar w katalogu projektu: $PSScriptRoot"
}

$args = @($phpUnitPhar, "--do-not-cache-result") + $PhpUnitArgs

Write-Host ""
Write-Host "================================"
Write-Host "  Testy jednostkowe shopPRO"
Write-Host "================================"
Write-Host "PHP: $phpExe"
Write-Host "Cmd: $phpExe $($args -join ' ')"
Write-Host ""

& $phpExe @args
exit $LASTEXITCODE
