$tools = Get-ChildItem -Path "site\tools\*.html"

foreach ($tool in $tools) {
    $content = Get-Content $tool.FullName -Raw
    
    # Extract title and description
    $titleMatch = [regex]::Match($content, '<meta property="og:title" content="([^"]+)">')
    $descMatch = [regex]::Match($content, '<meta name="description" content="([^"]+)">')
    
    if ($titleMatch.Success -and $descMatch.Success) {
        $title = $titleMatch.Groups[1].Value
        $desc = $descMatch.Groups[1].Value
        
        $json = @{
            "@context" = "https://schema.org"
            "@type" = "WebApplication"
            "name" = $title
            "description" = $desc
            "applicationCategory" = "DeveloperApplication"
            "operatingSystem" = "All"
            "url" = "https://www.crawlie.app/tools/" + $tool.Name
            "offers" = @{
                "@type" = "Offer"
                "price" = "0"
                "priceCurrency" = "USD"
            }
        }
        
        $jsonStr = $json | ConvertTo-Json -Depth 5 -Compress
        $schemaTag = "<script type=`"application/ld+json`">$jsonStr</script>`n</head>"
        
        # Don't inject if it's already there
        if (-not $content.Contains("WebApplication")) {
            $newContent = $content -replace "(?i)</head>", $schemaTag
            if ($content -ne $newContent) {
                Set-Content -Path $tool.FullName -Value $newContent -Encoding UTF8
            }
        }
    }
}
Write-Host "Done adding WebApplication schema to tools."
