# Script to add missing advanced elements to all Academic templates
# Adds: del, sup, sub, mark, footnote, footnoteRef

$academicTemplates = @(
    "academic\academic-journal.json",
    "academic\arctic-base.json",
    "academic\botanical-textbook.json",
    "academic\chemistry-lab.json",
    "academic\dark-academia.json",
    "academic\education-friendly.json",
    "academic\emergency-room.json",
    "academic\furniture-manual.json",
    "academic\history-textbook.json",
    "academic\kids-education.json",
    "academic\mathematics-paper.json",
    "academic\medical-professional.json",
    "academic\phd-thesis.json",
    "academic\scantron-test.json",
    "academic\scientific-journal.json",
    "academic\weather-radar.json"
)

$advancedElements = @{
    "del" = @{
        "_comment_del" = "~~strikethrough~~ - deleted text"
        "del" = @{
            "font" = "body"
            "strikethrough" = $true
        }
    }
    "sup" = @{
        "_comment_sup" = "^superscript^ - superscript text"
        "sup" = @{
            "font" = "body"
            "size" = 8
            "superScript" = $true
        }
    }
    "sub" = @{
        "_comment_sub" = "~subscript~ - subscript text"
        "sub" = @{
            "font" = "body"
            "size" = 8
            "subScript" = $true
        }
    }
    "mark" = @{
        "_comment_mark" = "==highlighted text== - marked/highlighted text"
        "mark" = @{
            "font" = "body"
            "background" = "accent"
            "color" = "FFFFFF"
        }
    }
    "footnote" = @{
        "_comment_footnote" = "[^1] - footnote references"
        "footnote" = @{
            "font" = "body"
            "size" = 9
            "color" = "textSecondary"
            "superScript" = $true
        }
    }
    "footnoteRef" = @{
        "_comment_footnote_ref" = "Footnote content at bottom of page"
        "footnoteRef" = @{
            "font" = "body"
            "size" = 9
            "color" = "textSecondary"
            "spacing" = @{
                "before" = 6
                "after" = 6
                "line" = 1.2
            }
        }
    }
}

foreach ($templatePath in $academicTemplates) {
    $fullPath = "src-tauri\templates\$templatePath"
    
    try {
        $content = Get-Content $fullPath -Raw | ConvertFrom-Json
        
        # Add missing elements
        $modified = $false
        foreach ($elementName in @('del', 'sup', 'sub', 'mark', 'footnote', 'footnoteRef')) {
            if (-not $content.elements.PSObject.Properties.Name.Contains($elementName)) {
                $elementData = $advancedElements[$elementName]
                foreach ($prop in $elementData.Keys) {
                    $content.elements | Add-Member -MemberType NoteProperty -Name $prop -Value $elementData[$prop] -Force
                }
                $modified = $true
            }
        }
        
        if ($modified) {
            # Write back with proper formatting
            $json = $content | ConvertTo-Json -Depth 10
            [System.IO.File]::WriteAllLines($fullPath, $json)
            Write-Host "✅ Updated: $templatePath" -ForegroundColor Green
        } else {
            Write-Host "⏭️  Skipped: $templatePath (already complete)" -ForegroundColor Yellow
        }
        
    } catch {
        Write-Host "❌ Error updating $templatePath`: $_" -ForegroundColor Red
    }
}

Write-Host "`n✅ Academic templates update complete!" -ForegroundColor Green
