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

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

    $titleMatch = [regex]::Match($html, '<title>(.*?)</title>')
    if (!$titleMatch.Success) { continue }
    $title = $titleMatch.Groups[1].Value
    
    # Safely remove "Free ", " tool", and the "- Crawlie" suffix using \W (non-word characters) to avoid eating letters
    $toolName = $title -replace '(?i)^Free\s+', '' -replace '(?i)\s+tool$', '' -replace '(?i)\s*\W{1,4}\s*Crawlie\s*$', ''
    # Trim just in case
    $toolName = $toolName.Trim()

    $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>
"@

    # Replace the existing FAQ section with the new fixed one safely
    $html = $html -replace '(?s)<!-- SEO FAQ SECTION -->.*?</div>\s*</div>\s*(?=<div class="ad-container")', "$faqHtml`n"

    Set-Content -Path $file.FullName -Value $html -Encoding UTF8
    Write-Host "Fixed FAQ in $($file.Name) - Tool: $toolName"
}
