const fs = require('fs');
const path = require('path');
const { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell } = require('docx');

async function createHybridDynamicTemplate() {
    const doc = new Document({
        sections: [{
            properties: {},
            children: [
                new Paragraph({
                    children: [new TextRun({ text: "HYBRID DYNAMISCHE TABELLEN", bold: true, size: 32 })],
                }),
                new Paragraph({ children: [new TextRun({ text: "" })] }),
                new Paragraph({ children: [new TextRun({ text: "Projekt: ++projekt++" })] }),
                new Paragraph({ children: [new TextRun({ text: "Datum: ++datum++" })] }),
                new Paragraph({ children: [new TextRun({ text: "" })] }),
                new Paragraph({ children: [new TextRun({ text: "MITARBEITER:", bold: true, size: 24 })] }),
                new Table({
                    rows: [
                        // Header Row
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph({ children: [new TextRun({ text: "Nr.", bold: true })] })] }),
                                new TableCell({ children: [new Paragraph({ children: [new TextRun({ text: "Name", bold: true })] })] }),
                                new TableCell({ children: [new Paragraph({ children: [new TextRun({ text: "Position", bold: true })] })] }),
                                new TableCell({ children: [new Paragraph({ children: [new TextRun({ text: "E-Mail", bold: true })] })] }),
                            ],
                        }),
                        // Bis zu 10 dynamische Zeilen (werden server-seitig gefüllt/leer gelassen)
                        ...Array.from({length: 10}, (_, i) => 
                            new TableRow({
                                children: [
                                    new TableCell({ children: [new Paragraph(`++mitarbeiter[${i}].nr++`)] }),
                                    new TableCell({ children: [new Paragraph(`++mitarbeiter[${i}].name++`)] }),
                                    new TableCell({ children: [new Paragraph(`++mitarbeiter[${i}].position++`)] }),
                                    new TableCell({ children: [new Paragraph(`++mitarbeiter[${i}].email++`)] }),
                                ],
                            })
                        )
                    ],
                }),
                new Paragraph({ children: [new TextRun({ text: "" })] }),
                new Paragraph({ children: [new TextRun({ text: "Status: ++status++" })] }),
            ],
        }],
    });

    const buffer = await Packer.toBuffer(doc);
    const templatePath = path.join(__dirname, 'templates', 'tabellen-template.docx');
    fs.writeFileSync(templatePath, buffer);
    
    console.log('✅ Hybrid-dynamisches Template erstellt (bis zu 10 Zeilen)');
    console.log('📋 Server füllt nur gewünschte Anzahl, Rest bleibt leer');
}

createHybridDynamicTemplate().catch(console.error);
