param(
    [Parameter(Mandatory = $true, Position = 0)]
    [string[]]$Urls,

    [string]$OutDir = ".\output",

    [string]$Hostname = "crawler.prototype.prolicht.digital",

    [int]$MaxImages = 5,

    [int]$Threads = 4,

    [int]$TimeoutSec = 300
)

$ErrorActionPreference = "Stop"
$hostBase = if ($Hostname.StartsWith("http")) { $Hostname } else { "https://$Hostname" }

Write-Host "Crawling $($Urls.Count) URLs (threads=$Threads)..."
$body = @{ urls = @($Urls); markdown = $true; images = $true; max_images = $MaxImages; threads = $Threads } | ConvertTo-Json
$result = Invoke-RestMethod -Uri "$hostBase/crawl/urls" -Method Post -ContentType "application/json" -Body $body -TimeoutSec $TimeoutSec

Write-Host "  status: $($result.status)"
Write-Host "  total: $($result.total)"
Write-Host "  succeeded: $($result.succeeded)"
Write-Host "  failed: $($result.failed)"

if ($result.failed_urls) {
    Write-Host "`nFailed URLs:"
    foreach ($u in $result.failed_urls) { Write-Host "  $u" }
}

# --- download markdown for each succeeded URL via individual /crawl/single requests ---
$scriptPath = Join-Path $PSScriptRoot "crawl_and_download.ps1"
if (Test-Path $scriptPath) {
    Write-Host "`nDownloading markdown + images for each succeeded URL..."
    foreach ($pair in $result.results.GetEnumerator()) {
        if ($pair.Value -eq $true) {
            Write-Host "`n--- $($pair.Key) ---"
            & $scriptPath -Url $pair.Key -OutDir $OutDir -Hostname $Hostname -MaxImages $MaxImages -TimeoutSec $TimeoutSec
        }
    }
}
