$reportPath = "site\report.html"
$sitemapPath = "site\tools\sitemap-generator.html"

# Get HTML of both files
$reportHtml = Get-Content $reportPath -Raw
$sitemapHtml = Get-Content $sitemapPath -Raw

# 1. Extract the blog slider from sitemap-generator.html
# The blog slider starts with <div class="wrap-mid" style="padding-bottom:60px;" data-noprint>
# and ends right before <footer class="footer wrap" data-noprint>
$blogRegex = '(?s)(<div class="wrap-mid" style="padding-bottom:60px;" data-noprint>.*?)<footer'
$blogMatch = [regex]::Match($sitemapHtml, $blogRegex)
$blogHtml = ""
if ($blogMatch.Success) {
    $blogHtml = $blogMatch.Groups[1].Value
}

# 2. Extract the FAQ from sitemap-generator.html
# The FAQ starts with <!-- SEO FAQ SECTION --> and ends before <div class="wrap-mid" data-noprint><div class="ad-container"
$faqRegex = '(?s)(<!-- SEO FAQ SECTION -->.*?)<div class="wrap-mid" data-noprint>\s*<div class="ad-container"'
$faqMatch = [regex]::Match($sitemapHtml, $faqRegex)
$faqHtml = ""
if ($faqMatch.Success) {
    $faqHtml = $faqMatch.Groups[1].Value
    # Rename "XML Sitemap Generator" to "SEO Audit Report"
    $faqHtml = $faqHtml -replace 'XML Sitemap Generator', 'SEO Audit Report'
}

# Now inject both into report.html
# report.html currently has:
# </aside>
# </div>
# <footer class="footer wrap" data-noprint>
# We want to inject the FAQ and Blog Slider right before the footer!

$injection = "$faqHtml`n$blogHtml`n<footer"
$reportHtml = $reportHtml -replace '<footer', $injection

Set-Content -Path $reportPath -Value $reportHtml -Encoding UTF8
Write-Host "Successfully injected FAQ and Blog slider into report.html"
