param(
    [string]$DockerHost = "ssh://docker-gpu.cin.su",
    [string]$ComposeFile = "core/deploy/docker-gpu/embeddings/compose.yaml",
    [string]$EnvFile = "core/deploy/docker-gpu/embeddings/.env.example",
    [string]$BaseUrl = "http://docker-gpu.cin.su:8082",
    [string]$ExpectedModel = "qwen3-embedding-0.6b",
    [int]$WaitSeconds = 900,
    [switch]$ConfigOnly,
    [switch]$Pull,
    [switch]$Down
)

$ErrorActionPreference = "Stop"

if (-not (Test-Path -LiteralPath $ComposeFile)) {
    throw "Compose file not found: $ComposeFile"
}

if (-not (Test-Path -LiteralPath $EnvFile)) {
    throw "Env file not found: $EnvFile"
}

$composeArgs = @(
    "--host", $DockerHost,
    "compose",
    "--env-file", $EnvFile,
    "-f", $ComposeFile
)

if ($ConfigOnly) {
    docker @composeArgs config
    exit $LASTEXITCODE
}

if ($Down) {
    docker @composeArgs down
    exit $LASTEXITCODE
}

if ($Pull) {
    docker @composeArgs pull
    if ($LASTEXITCODE -ne 0) {
        exit $LASTEXITCODE
    }
}

docker @composeArgs up -d
if ($LASTEXITCODE -ne 0) {
    exit $LASTEXITCODE
}

$deadline = [DateTimeOffset]::UtcNow.AddSeconds($WaitSeconds)
$lastError = ""
do {
    try {
        $health = Invoke-RestMethod -Method Get -Uri "$($BaseUrl.TrimEnd('/'))/health" -TimeoutSec 10
        if ($health.status -eq "ok") {
            $models = Invoke-RestMethod -Method Get -Uri "$($BaseUrl.TrimEnd('/'))/v1/models" -TimeoutSec 10
            $modelIds = @($models.data | ForEach-Object { $_.id })
            if ($modelIds -notcontains $ExpectedModel) {
                throw "Expected model '$ExpectedModel' is absent. Loaded: $($modelIds -join ', ')"
            }

            $body = @{
                model = $ExpectedModel
                input = @("поиск процедуры проведения документа 1С")
            } | ConvertTo-Json -Depth 4
            $embedding = Invoke-RestMethod `
                -Method Post `
                -Uri "$($BaseUrl.TrimEnd('/'))/v1/embeddings" `
                -ContentType "application/json; charset=utf-8" `
                -Body ([Text.Encoding]::UTF8.GetBytes($body)) `
                -TimeoutSec 120
            $dimensions = @($embedding.data[0].embedding).Count
            if ($dimensions -le 0) {
                throw "Embedding endpoint returned an empty vector."
            }
            Write-Host "Embedding endpoint is ready: model=$ExpectedModel dimensions=$dimensions url=$BaseUrl"
            exit 0
        }
    }
    catch {
        $lastError = $_.Exception.Message
    }
    Start-Sleep -Seconds 5
} while ([DateTimeOffset]::UtcNow -lt $deadline)

docker @composeArgs logs --tail 100
throw "Embedding endpoint did not become ready in $WaitSeconds seconds. Last error: $lastError"
