const fs = require('fs');
const Docxtemplater = require('docxtemplater');
const PizZip = require('pizzip');

// Test mit docxtemplater
function testDocxtemplater() {
    // Beispiel-Template-Inhalt (normalerweise aus .docx gelesen)
    const content = fs.readFileSync('./templates/simple-template.docx', 'binary');
    const zip = new PizZip(content);
    
    const doc = new Docxtemplater(zip, {
        paragraphLoop: true,
        linebreaks: true,
    });
    
    // Echte dynamische Daten
    const data = {
        projekt: "Dynamisches Projekt",
        datum: new Date().toLocaleDateString('de-DE'),
        mitarbeiter: [
            { nr: 1, name: "Max Mustermann", position: "Developer", email: "max@example.com" },
            { nr: 2, name: "Anna Schmidt", position: "Designer", email: "anna@example.com" },
            { nr: 3, name: "Tom Weber", position: "Manager", email: "tom@example.com" },
            { nr: 4, name: "Lisa König", position: "Analyst", email: "lisa@example.com" },
            { nr: 5, name: "Jan Peters", position: "Tester", email: "jan@example.com" }
        ],
        status: "Aktiv"
    };
    
    console.log('📊 Teste mit docxtemplater und', data.mitarbeiter.length, 'Mitarbeitern');
    
    try {
        doc.render(data);
        
        const buf = doc.getZip().generate({
            type: 'nodebuffer',
            compression: 'DEFLATE',
        });
        
        fs.writeFileSync('./output/docxtemplater_test.docx', buf);
        console.log('✅ docxtemplater Test erfolgreich!');
        
    } catch (error) {
        console.log('❌ docxtemplater Fehler:', error.message);
    }
}

testDocxtemplater();