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

// Erstelle ein Template mit Tabelle für Listen
async function createTableTemplate() {
    const doc = new Document({
        sections: [{
            properties: {},
            children: [
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "RECHNUNG",
                            bold: true,
                            size: 32,
                        }),
                    ],
                }),
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "",
                        }),
                    ],
                }),
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "Kunde: ++name++",
                        }),
                    ],
                }),
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "E-Mail: ++email++",
                        }),
                    ],
                }),
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "Datum: ++date++",
                        }),
                    ],
                }),
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "",
                        }),
                    ],
                }),
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "ARTIKEL",
                            bold: true,
                        }),
                    ],
                }),
                // Einfache Tabelle für Artikel
                new Table({
                    rows: [
                        new TableRow({
                            children: [
                                new TableCell({
                                    children: [new Paragraph("Produkt")],
                                }),
                                new TableCell({
                                    children: [new Paragraph("Menge")],
                                }),
                                new TableCell({
                                    children: [new Paragraph("Preis")],
                                }),
                            ],
                        }),
                        new TableRow({
                            children: [
                                new TableCell({
                                    children: [new Paragraph("++items[0].product++")],
                                }),
                                new TableCell({
                                    children: [new Paragraph("++items[0].quantity++")],
                                }),
                                new TableCell({
                                    children: [new Paragraph("++items[0].price++€")],
                                }),
                            ],
                        }),
                        new TableRow({
                            children: [
                                new TableCell({
                                    children: [new Paragraph("++items[1].product++")],
                                }),
                                new TableCell({
                                    children: [new Paragraph("++items[1].quantity++")],
                                }),
                                new TableCell({
                                    children: [new Paragraph("++items[1].price++€")],
                                }),
                            ],
                        }),
                    ],
                }),
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "",
                        }),
                    ],
                }),
                new Paragraph({
                    children: [
                        new TextRun({
                            text: "Gesamtsumme: ++total++€",
                            bold: true,
                        }),
                    ],
                }),
            ],
        }],
    });

    const buffer = await Packer.toBuffer(doc);
    const outputPath = path.join(__dirname, 'templates', 'tabellen-template.docx');
    fs.writeFileSync(outputPath, buffer);
    
    console.log('✅ Tabellen-Template erstellt:', outputPath);
}

createTableTemplate().catch(console.error);