# PowerShell equivalent of start-ollama-cv.sh
# Starts Ollama service, pulls model if needed, waits, then restarts AI service

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# Change to the parent directory of scripts
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location (Join-Path $scriptDir '..')

$MODEL = if ($env:OLLAMA_MODEL) { $env:OLLAMA_MODEL } else { 'qwen2.5:7b' }
$OLLAMA_WAIT_SECONDS = if ($env:OLLAMA_WAIT_SECONDS) { [int]$env:OLLAMA_WAIT_SECONDS } else { 180 }
$PULL_WAIT_SECONDS = if ($env:OLLAMA_PULL_WAIT_SECONDS) { [int]$env:OLLAMA_PULL_WAIT_SECONDS } else { 1800 }

function compose {
    docker compose @args
}

function wait_for_ollama {
    $deadline = (Get-Date).AddSeconds($OLLAMA_WAIT_SECONDS)
    while ((Get-Date) -lt $deadline) {
        try {
            compose exec -T ollama ollama list | Out-Null
            return $true
        } catch {
            # Ignore errors, just wait
        }
        Start-Sleep -Seconds 3
    }
    return $false
}

function model_present {
    try {
        $models = compose exec -T ollama ollama list 2>$null | Select-Object -Skip 1 | ForEach-Object { $_.Split()[0] }
        return $models -contains $MODEL
    } catch {
        return $false
    }
}

function wait_for_model {
    $deadline = (Get-Date).AddSeconds($PULL_WAIT_SECONDS)
    while ((Get-Date) -lt $deadline) {
        if (model_present) {
            return $true
        }
        Start-Sleep -Seconds 5
    }
    return $false
}

Write-Host "Starting Ollama service..."
compose up -d ollama

if (-not (wait_for_ollama)) {
    Write-Host "Ollama did not become ready within ${OLLAMA_WAIT_SECONDS}s."
    try { compose logs --tail=200 ollama } catch { }
    exit 1
}

Write-Host "Ollama is responding."

if (model_present) {
    Write-Host "Model already present: $MODEL"
} else {
    Write-Host "Pulling Ollama model: $MODEL"
    try {
        compose exec -T ollama ollama pull $MODEL
    } catch {
        Write-Host "Model pull command failed."
        try { compose logs --tail=200 ollama } catch { }
        exit 1
    }
}

if (-not (wait_for_model)) {
    Write-Host "Model ${MODEL} did not appear within ${PULL_WAIT_SECONDS}s."
    try { compose exec -T ollama ollama list } catch { }
    exit 1
}

Write-Host "Ollama model ready: $MODEL"

Write-Host "Restarting AI service so it can use the ready Ollama model."
compose up -d ai-service

try {
    $state = compose ps ai-service --format '{{.State}}' 2>$null | Select-Object -First 1 | ForEach-Object { $_.ToLower().Trim() }
    if ($state -ne 'running') {
        Write-Host "AI service is not running after Ollama warmup."
        try { compose logs --tail=200 ai-service } catch { }
        exit 1
    }
} catch {
    Write-Host "Failed to check AI service status."
    exit 1
}

Write-Host "Ollama warmup complete."