const fs = require("fs");
const { PDFDocument, rgb, degrees } = require("pdf-lib");

exports.addWatermark = async (filePath, text, color) => {
  try {
    const existingPdfBytes = fs.readFileSync(filePath);

    const pdfDoc = await PDFDocument.load(existingPdfBytes);

    const pages = pdfDoc.getPages();

    pages.forEach((page) => {
      const { width, height } = page.getSize();

      page.drawText(text, {
        x: width / 4,
        y: height / 2,

        size: 80,
        rotate: degrees(-30),

        color,

        opacity: 0.25,
      });
    });

    const pdfBytes = await pdfDoc.save();

    fs.writeFileSync(filePath, pdfBytes);
  } catch (err) {
    console.error("❌ PDF Watermark Fehler:", err);
  }
};
