$articles = Get-ChildItem -Path "site\blog\articles\*.html"
$images = @("seo_dashboard.png", "content_marketing.png", "technical_seo.png", "ecommerce_growth.png")

foreach ($article in $articles) {
    $content = Get-Content $article.FullName -Raw
    
    # Deterministic image selection based on filename length
    $imgIndex = $article.Name.Length % 4
    $imgName = $images[$imgIndex]
    
    $imgTag = "</p><img src=`"../../assets/blog-images/generated/$imgName`" alt=`"SEO illustration`" width=`"800`" height=`"450`" loading=`"lazy`" style=`"width:100%;height:auto;border-radius:12px;margin:12px 0 24px;display:block;`"><p"
    
    # We only want to replace the first occurrence of </p><p
    $regex = [regex]::Escape("</p><p")
    $newContent = [regex]::Replace($content, $regex, $imgTag, 1)
    
    if ($content -ne $newContent) {
        Set-Content -Path $article.FullName -Value $newContent -Encoding UTF8
    }
}
Write-Host "Done adding images to blog articles."
