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

async function createTableTemplate() {
    const doc = new Document({
        sections: [{
            properties: {},
            children: [
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "TABELLEN DEMO DOKUMENT",
                            bold: true,
                            size: 32,
                        }),
                    ],
                }),
                new Paragraph({ children: [new TextRun({ text: "" })] }),
                new Paragraph({ children: [new TextRun({ text: "Projekt: ++projekt++", bold: true })] }),
                new Paragraph({ children: [new TextRun({ text: "Datum: ++datum++" })] }),
                new Paragraph({ children: [new TextRun({ text: "Ersteller: ++ersteller++" })] }),
                new Paragraph({ children: [new TextRun({ text: "" })] }),
                new Paragraph({
                    children: [new TextRun({ text: "MITARBEITERLISTE:", bold: true, size: 24 })],
                }),
                new Table({
                    rows: [
                        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: "Abteilung", bold: true })] })] }),
                                new TableCell({ children: [new Paragraph({ children: [new TextRun({ text: "E-Mail", bold: true })] })] }),
                            ],
                        }),
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph("++INS mitarbeiterRows++Nr: ++nr++")] }),
                                new TableCell({ children: [new Paragraph("++name++")] }),
                                new TableCell({ children: [new Paragraph("++position++")] }),
                                new TableCell({ children: [new Paragraph("++abteilung++")] }),
                                new TableCell({ children: [new Paragraph("++email++++/INS++")] }),
                            ],
                        }),
                    ],
                }),
                new Paragraph({ children: [new TextRun({ text: "" })] }),
                new Paragraph({ children: [new TextRun({ text: "PROJEKTAUFGABEN:", bold: true, size: 24 })] }),
                new Table({
                    rows: [
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph({ children: [new TextRun({ text: "Nr.", bold: true })] })] }),
                                new TableCell({ children: [new Paragraph({ children: [new TextRun({ text: "Aufgabe", bold: true })] })] }),
                                new TableCell({ children: [new Paragraph({ children: [new TextRun({ text: "Status", bold: true })] })] }),
                                new TableCell({ children: [new Paragraph({ children: [new TextRun({ text: "Deadline", bold: true })] })] }),
                            ],
                        }),
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph("++INS aufgabenRows++Nr: ++nr++")] }),
                                new TableCell({ children: [new Paragraph("++titel++")] }),
                                new TableCell({ children: [new Paragraph("++status++")] }),
                                new TableCell({ children: [new Paragraph("++deadline++++/INS++")] }),
                            ],
                        }),
                    ],
                }),
                new Paragraph({ children: [new TextRun({ text: "" })] }),
                new Paragraph({ children: [new TextRun({ text: "Zusammenfassung: ++zusammenfassung++" })] }),
                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('✅ Tabellen-Template mit LOOP-Syntax erstellt');
}

createTableTemplate().catch(console.error);
