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

async function createWorkingTableTemplate() {
    const doc = new Document({
        sections: [{
            properties: {},
            children: [
                new Paragraph({
                    children: [new TextRun({ text: "DYNAMISCHE TABELLEN DEMO", 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
                        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 })] })] }),
                            ],
                        }),
                        // Dynamische Zeilen - Row 0
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph("++mitarbeiter[0].nr++")] }),
                                new TableCell({ children: [new Paragraph("++mitarbeiter[0].name++")] }),
                                new TableCell({ children: [new Paragraph("++mitarbeiter[0].position++")] }),
                                new TableCell({ children: [new Paragraph("++mitarbeiter[0].email++")] }),
                            ],
                        }),
                        // Dynamische Zeilen - Row 1
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph("++mitarbeiter[1].nr++")] }),
                                new TableCell({ children: [new Paragraph("++mitarbeiter[1].name++")] }),
                                new TableCell({ children: [new Paragraph("++mitarbeiter[1].position++")] }),
                                new TableCell({ children: [new Paragraph("++mitarbeiter[1].email++")] }),
                            ],
                        }),
                        // Dynamische Zeilen - Row 2
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph("++mitarbeiter[2].nr++")] }),
                                new TableCell({ children: [new Paragraph("++mitarbeiter[2].name++")] }),
                                new TableCell({ children: [new Paragraph("++mitarbeiter[2].position++")] }),
                                new TableCell({ children: [new Paragraph("++mitarbeiter[2].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('✅ Funktionierendes Tabellen-Template mit Array-Indizes erstellt');
    console.log('📋 Syntax: ++mitarbeiter[0].feldname++, ++mitarbeiter[1].feldname++, etc.');
}

createWorkingTableTemplate().catch(console.error);
