$tsDir = "d:\aoyiduo\woterm\woterm\private\languages\ts"
$files = Get-ChildItem $tsDir -Filter "woterm_*.ts.xml" | Sort-Object Name

Write-Host "=== Translation File Analysis ===" -ForegroundColor Cyan
Write-Host ""

function Get-TranslationText {
    param($transNode)
    if ($null -eq $transNode) { return $null }
    
    if ($transNode -is [string]) {
        if ([string]::IsNullOrWhiteSpace($transNode)) { return $null }
        return $transNode
    }
    
    if ($transNode.InnerText -and -not [string]::IsNullOrWhiteSpace($transNode.InnerText)) {
        return $transNode.InnerText
    }
    
    if ($transNode.'#text' -and -not [string]::IsNullOrWhiteSpace($transNode.'#text')) {
        return $transNode.'#text'
    }
    
    return $null
}

$enFile = Join-Path $tsDir "woterm_en.ts.xml"
$enXml = [xml](Get-Content $enFile -Encoding UTF8)
$enContexts = @{}
$enAllSources = @()
foreach ($ctx in $enXml.TS.context) {
    $ctxName = $ctx.name
    $messages = @{}
    foreach ($msg in $ctx.message) {
        $src = $msg.source
        $messages[$src] = $true
        $enAllSources += "$ctxName|$src"
    }
    $enContexts[$ctxName] = $messages
}
$totalEnMessages = $enAllSources.Count
Write-Host "English (base): $totalEnMessages messages in $($enContexts.Count) contexts" -ForegroundColor Green
Write-Host ""

foreach ($file in $files) {
    if ($file.Name -eq "woterm_en.ts.xml") { continue }
    
    $filePath = $file.FullName
    $langCode = $file.Name -replace 'woterm_(.*)\.ts\.xml', '$1'
    
    try {
        $xml = [xml](Get-Content $filePath -Encoding UTF8)
        $totalTranslated = 0
        $totalEmpty = 0
        $totalInEn = 0
        $totalNotInEn = 0
        $missingFromEn = @()
        $emptyTranslations = @()
        
        $allContexts = @{}
        foreach ($ctx in $xml.TS.context) {
            $ctxName = $ctx.name
            $allContexts[$ctxName] = @{}
            foreach ($msg in $ctx.message) {
                $src = $msg.source
                $transText = Get-TranslationText $msg.translation
                $allContexts[$ctxName][$src] = $transText
                
                $fullKey = "$ctxName|$src"
                if ($enAllSources -contains $fullKey) {
                    $totalInEn++
                } else {
                    $totalNotInEn++
                }
                
                if ($null -ne $transText) {
                    $totalTranslated++
                } else {
                    $totalEmpty++
                    if ($emptyTranslations.Count -lt 15) {
                        $emptyTranslations += "  [$ctxName] $src"
                    }
                }
            }
        }
        
        $totalMessages = $totalTranslated + $totalEmpty
        $pct = if ($totalMessages -gt 0) { [math]::Round(($totalTranslated / $totalMessages) * 100, 1) } else { 0 }
        
        Write-Host "$($file.Name):" -ForegroundColor Yellow
        Write-Host "  Total messages: $totalMessages"
        Write-Host "  Translated: $totalTranslated ($pct%)"
        Write-Host "  Empty/Missing: $totalEmpty"
        Write-Host "  In English base: $totalInEn"
        Write-Host "  Extra (not in EN): $totalNotInEn"
        
        if ($emptyTranslations.Count -gt 0) {
            Write-Host "  Sample empty translations:"
            foreach ($item in $emptyTranslations) {
                Write-Host $item -ForegroundColor Gray
            }
        }
        Write-Host ""
    }
    catch {
        Write-Host "$($file.Name): ERROR - $_" -ForegroundColor Red
        Write-Host ""
    }
}

Write-Host "=== End of Analysis ===" -ForegroundColor Cyan
