# NNUE Pipeline launcher for PowerShell (Windows)

Write-Host "Launching NNUE Training Pipeline..." -ForegroundColor Green
Write-Host ""

# Check if we're in the right directory
if (!(Test-Path "python")) {
    Write-Host "ERROR: python directory not found" -ForegroundColor Red
    Write-Host "Please run this script from the modules\bot directory" -ForegroundColor Red
    exit 1
}

# Check for Stockfish
$stockfishPath = $env:STOCKFISH_PATH
if ($null -eq $stockfishPath -or $stockfishPath -eq "") {
    Write-Host "Stockfish path not set. Trying to find in PATH..." -ForegroundColor Yellow
    $stockfishPath = (Get-Command stockfish -ErrorAction SilentlyContinue).Source
    if ($null -eq $stockfishPath) {
        Write-Host "Stockfish not found in PATH" -ForegroundColor Yellow
        Write-Host "Set STOCKFISH_PATH environment variable and try again:" -ForegroundColor Yellow
        Write-Host '  $env:STOCKFISH_PATH = "C:\path\to\stockfish.exe"' -ForegroundColor Cyan
    } else {
        Write-Host "Found Stockfish: $stockfishPath" -ForegroundColor Green
        $env:STOCKFISH_PATH = $stockfishPath
    }
}

# Run the pipeline
Write-Host "Running pipeline from: $(Get-Location)\python" -ForegroundColor Cyan
Write-Host ""

Push-Location python
try {
    # Use bash if available (Git Bash or WSL)
    if (Get-Command bash -ErrorAction SilentlyContinue) {
        Write-Host "Using bash script..." -ForegroundColor Cyan
        bash ./run_pipeline.sh
    } else {
        Write-Host "Using batch script..." -ForegroundColor Cyan
        & cmd.exe /c run_pipeline.bat
    }
    $result = $LASTEXITCODE
} finally {
    Pop-Location
}

if ($result -eq 0) {
    Write-Host ""
    Write-Host "Pipeline completed successfully!" -ForegroundColor Green
} else {
    Write-Host ""
    Write-Host "Pipeline failed with exit code $result" -ForegroundColor Red
}

exit $result
