# Template Audit Script
# Analyzes all templates against research-paper.json reference

$reference = Get-Content "src-tauri\templates\academic\research-paper.json" | ConvertFrom-Json
$templateFiles = Get-ChildItem -Path "src-tauri\templates" -Recurse -Filter "*.json"

$requiredElements = @('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'blockquote', 'code', 'pre', 
                       'ul', 'ol', 'li', 'strong', 'em', 'a', 'table', 'th', 'td', 
                       'hr', 'img', 'del', 'sup', 'sub', 'mark', 'footnote', 'footnoteRef')

$requiredColors = @('text', 'textSecondary', 'background', 'accent', 'border', 'codeBg', 'blockquoteBorder')
$requiredFonts = @('heading', 'body', 'code')

Write-Host "`n=== TEMPLATE AUDIT REPORT ===" -ForegroundColor Cyan
Write-Host "Reference: research-paper.json with $($requiredElements.Count) elements`n" -ForegroundColor Green

$issues = @()

foreach ($file in $templateFiles) {
    try {
        $template = Get-Content $file.FullName | ConvertFrom-Json
        $templateName = "$($template.category)/$($template.id)"
        
        # Check elements
        $missingElements = @()
        $elementKeys = if ($template.elements) { $template.elements.PSObject.Properties.Name | Where-Object { $_ -notlike "_comment*" } } else { @() }
        
        foreach ($element in $requiredElements) {
            if ($element -notin $elementKeys) {
                $missingElements += $element
            }
        }
        
        # Check colors
        $missingColors = @()
        $colorKeys = if ($template.typography.colors) { $template.typography.colors.PSObject.Properties.Name | Where-Object { $_ -notlike "_comment*" } } else { @() }
        
        foreach ($color in $requiredColors) {
            if ($color -notin $colorKeys) {
                $missingColors += $color
            }
        }
        
        # Check fonts
        $missingFonts = @()
        $fontKeys = if ($template.typography.fonts) { $template.typography.fonts.PSObject.Properties.Name | Where-Object { $_ -notlike "_comment*" } } else { @() }
        
        foreach ($font in $requiredFonts) {
            if ($font -notin $fontKeys) {
                $missingFonts += $font
            }
        }
        
        # Report issues
        if ($missingElements.Count -gt 0 -or $missingColors.Count -gt 0 -or $missingFonts.Count -gt 0) {
            $issue = [PSCustomObject]@{
                Template = $templateName
                MissingElements = $missingElements.Count
                MissingColors = $missingColors.Count
                MissingFonts = $missingFonts.Count
                ElementsList = ($missingElements -join ', ')
                ColorsList = ($missingColors -join ', ')
                FontsList = ($missingFonts -join ', ')
            }
            $issues += $issue
            
            Write-Host "❌ $templateName" -ForegroundColor Red
            if ($missingElements.Count -gt 0) {
                Write-Host "   Missing $($missingElements.Count) elements: $($missingElements -join ', ')" -ForegroundColor Yellow
            }
            if ($missingColors.Count -gt 0) {
                Write-Host "   Missing $($missingColors.Count) colors: $($missingColors -join ', ')" -ForegroundColor Yellow
            }
            if ($missingFonts.Count -gt 0) {
                Write-Host "   Missing $($missingFonts.Count) fonts: $($missingFonts -join ', ')" -ForegroundColor Yellow
            }
        } else {
            Write-Host "✅ $templateName" -ForegroundColor Green
        }
        
    } catch {
        Write-Host "❌ Error parsing $($file.Name): $_" -ForegroundColor Red
    }
}

Write-Host "`n=== SUMMARY ===" -ForegroundColor Cyan
Write-Host "Total templates: $($templateFiles.Count)" -ForegroundColor White
Write-Host "Templates with issues: $($issues.Count)" -ForegroundColor Yellow
Write-Host "Templates complete: $($templateFiles.Count - $issues.Count)" -ForegroundColor Green

if ($issues.Count -gt 0) {
    Write-Host "`nTemplates needing fixes:" -ForegroundColor Yellow
    $issues | Format-Table Template, MissingElements, MissingColors, MissingFonts -AutoSize
}
