$files = Get-ChildItem -Path "site\tools\*.html"
$files += Get-Item "site\report.html"

foreach ($file in $files) {
    $html = Get-Content $file.FullName -Raw

    # Prevent double-injection
    if ($html -match "id=`"seo-faq`"") {
        Write-Host "Skipping $($file.Name) - FAQ already exists"
        continue
    }

    # Extract Title for the Tool Name
    $titleMatch = [regex]::Match($html, '<title>(.*?)</title>')
    if (!$titleMatch.Success) { continue }
    $title = $titleMatch.Groups[1].Value
    # Clean up title: e.g. "Free XML Sitemap Generator — Crawlie" -> "XML Sitemap Generator"
    $toolName = $title -replace '(?i)Free ', '' -replace ' — Crawlie', '' -replace ' tool', '' -replace ' Tool', ''

    $faqHtml = @"
<!-- SEO FAQ SECTION -->
<div class="wrap-mid" id="seo-faq" data-noprint style="padding-top:40px;padding-bottom:10px;">
  <div class="card" style="padding:32px;">
    <h2 style="font-size:22px;margin-bottom:20px;letter-spacing:-0.01em;">Frequently Asked Questions</h2>
    
    <details class="faq-item" style="margin-bottom:16px;border-bottom:1px solid var(--border);padding-bottom:16px;">
      <summary style="font-weight:700;font-size:15px;cursor:pointer;color:var(--ink);list-style:none;display:flex;justify-content:space-between;align-items:center;">
        What is the $toolName and how does it work? <span style="color:var(--muted-2);font-size:20px;">+</span>
      </summary>
      <p class="muted" style="margin-top:12px;font-size:14.5px;line-height:1.6;">The $toolName is a powerful, browser-based utility designed to streamline your SEO workflow. It processes your inputs entirely in your browser or through secure edge relays, delivering instant, accurate results without storing your data.</p>
    </details>

    <details class="faq-item" style="margin-bottom:16px;border-bottom:1px solid var(--border);padding-bottom:16px;">
      <summary style="font-weight:700;font-size:15px;cursor:pointer;color:var(--ink);list-style:none;display:flex;justify-content:space-between;align-items:center;">
        How do I use the $toolName? <span style="color:var(--muted-2);font-size:20px;">+</span>
      </summary>
      <p class="muted" style="margin-top:12px;font-size:14.5px;line-height:1.6;">Simply enter your URL or required data into the input field above and click the action button. The $toolName will automatically analyze your input and generate a comprehensive, exportable report or code snippet in seconds.</p>
    </details>

    <details class="faq-item" style="margin-bottom:16px;border-bottom:1px solid var(--border);padding-bottom:16px;">
      <summary style="font-weight:700;font-size:15px;cursor:pointer;color:var(--ink);list-style:none;display:flex;justify-content:space-between;align-items:center;">
        Is the $toolName completely free and unlimited? <span style="color:var(--muted-2);font-size:20px;">+</span>
      </summary>
      <p class="muted" style="margin-top:12px;font-size:14.5px;line-height:1.6;">Yes! Like all tools on Crawlie, the $toolName is 100% free to use. There are no paywalls, no login requirements, and absolutely no strict usage limits. You can run as many scans and generate as many reports as you need.</p>
    </details>
    
    <details class="faq-item" style="margin-bottom:0;padding-bottom:0;">
      <summary style="font-weight:700;font-size:15px;cursor:pointer;color:var(--ink);list-style:none;display:flex;justify-content:space-between;align-items:center;">
        Is my data safe and secure? <span style="color:var(--muted-2);font-size:20px;">+</span>
      </summary>
      <p class="muted" style="margin-top:12px;font-size:14.5px;line-height:1.6;">Absolutely. The $toolName runs its core logic locally in your browser. We do not store your website data, track your internal links, or keep a database of your reports.</p>
    </details>
  </div>
</div>

"@

    # Inject right before the main ad-container (we can match `<div class="ad-container"`)
    # Wait, some pages might have it inside <div class="wrap-mid">. 
    # Our FAQ is wrapped in a <div class="wrap-mid"> so we should inject it OUTSIDE any wrap-mid if possible, OR just replace <div class="ad-container" with FAQ + <div class="ad-container"
    # Actually, we can inject it right before `<div class="ad-container"` (but wait, report.html has it at line 143: `<div class="ad-container" data-noprint>`)
    
    # Wait, we need to be careful to inject BEFORE the FIRST <div class="ad-container" in the main content.
    # report.html has `<div class="ad-container" data-noprint>`
    # tools/*.html have `<div class="ad-container" data-noprint>`
    
    # We will just replace the first instance of `<div class="ad-container"`
    $pattern = [regex]::Escape('<div class="ad-container"')
    $regex = New-Object System.Text.RegularExpressions.Regex($pattern, [System.Text.RegularExpressions.RegexOptions]::None)
    $html = $regex.Replace($html, "$faqHtml<div class=`"ad-container`"", 1)

    Set-Content -Path $file.FullName -Value $html -Encoding UTF8
    Write-Host "Injected FAQ into $($file.Name)"
}
