// const PDFDocument = require("pdfkit");
// const path = require("path");

// /**
//  * Industry-grade Payslip PDF
//  */
// const generatePayslipPdf = (res, data) => {
//     const { company, employee, month, year } = data;

//     const doc = new PDFDocument({
//         size: "A4",
//         margin: 40,
//     });

//     /* ================= RESPONSE HEADERS ================= */
//     res.setHeader("Content-Type", "application/pdf");
//     res.setHeader(
//         "Content-Disposition",
//         `attachment; filename=PAYSLIP-${employee.employeeId}-${month}-${year}.pdf`
//     );

//     doc.pipe(res);

//     const pageLeft = 40;
//     const pageRight = 555;
//     let y = 40;

//     /* ================= LOGO ================= */
//     const logoPath = path.resolve(
//         process.cwd(),
//         "uploads/images/company-logo.png"
//     );

//     try {
//         doc.image(logoPath, pageLeft, y, { width: 90 });
//     } catch (e) {
//         // logo optional
//     }

//     /* ================= COMPANY INFO ================= */
//     doc
//         .font("Helvetica-Bold")
//         .fontSize(14)
//         .text(company.name || "Service Nxt Pvt Ltd", pageLeft + 110, y + 5);

//     doc
//         .font("Helvetica")
//         .fontSize(9)
//         .text(company.address || "--", pageLeft + 110, y + 25)
//         .text(`GSTIN: ${company.gstin || "--"}`, pageLeft + 110, y + 40);

//     /* ================= PAYSLIP META (RIGHT) ================= */
//     doc
//         .font("Helvetica-Bold")
//         .fontSize(11)
//         .text("Payslip", pageLeft, y + 5, {
//             width: pageRight - pageLeft,
//             align: "right",
//         });

//     doc
//         .font("Helvetica")
//         .fontSize(9)
//         .text(
//             `${new Date(year, month - 1).toLocaleString("en-IN", {
//                 month: "long",
//                 year: "numeric",
//             })}`,
//             pageLeft,
//             y + 25,
//             {
//                 width: pageRight - pageLeft,
//                 align: "right",
//             }
//         );

//     y += 80;

//     /* ================= DIVIDER ================= */
//     //   doc.moveTo(pageLeft, y).lineTo(pageRight, y).stroke();
//     //   y += 20;

//     /* ================= EMPLOYEE DETAILS BOX ================= */
//     const boxTop = y;
//     const boxHeight = 110;
//     const midX = (pageLeft + pageRight) / 2;

//     // Outer box
//     doc.rect(pageLeft, boxTop, pageRight - pageLeft, boxHeight).stroke();

//     // Vertical divider
//     doc.moveTo(midX, boxTop).lineTo(midX, boxTop + boxHeight).stroke();

//     doc.font("Helvetica").fontSize(10);

//     /* -------- LEFT COLUMN -------- */
//     let ly = boxTop + 12;
//     doc.text(`Name: ${employee.name || "--"}`, pageLeft + 10, ly);
//     ly += 18;
//     doc.text(`Employee ID: ${employee.employeeId || "--"}`, pageLeft + 10, ly);
//     ly += 18;
//     doc.text(`Designation: ${employee.role || "--"}`, pageLeft + 10, ly);
//     ly += 18;
//     doc.text(`Department: ${employee.department || "--"}`, pageLeft + 10, ly);

//     /* -------- RIGHT COLUMN -------- */
//     let ry = boxTop + 12;
//     doc.text(`Bank: ${employee.bankName || "--"}`, midX + 10, ry);
//     ry += 18;
//     doc.text(`Account No: ${employee.bankAccount || "--"}`, midX + 10, ry);
//     ry += 18;
//     doc.text(`PAN: ${employee.pan || "--"}`, midX + 10, ry);
//     ry += 18;
//     doc.text(`PF / UAN: ${employee.pfUan || "--"}`, midX + 10, ry);

//     y += boxHeight + 30;

//     /* ================= EARNINGS & DEDUCTIONS TABLE ================= */
//     const tableTop = y;
//     const tableWidth = pageRight - pageLeft;
//     const colMid = pageLeft + tableWidth / 2;

//     // Outer table
//     doc.rect(pageLeft, tableTop, tableWidth, 120).stroke();
//     doc.moveTo(colMid, tableTop).lineTo(colMid, tableTop + 120).stroke();

//     /* -------- HEADERS -------- */
//     doc.font("Helvetica-Bold").fontSize(10);
//     doc.text("EARNINGS", pageLeft + 10, tableTop + 8);
//     doc.text("AMOUNT (₹)", colMid - 90, tableTop + 8, { align: "right" });

//     doc.text("DEDUCTIONS", colMid + 10, tableTop + 8);
//     doc.text("AMOUNT (₹)", pageRight - 90, tableTop + 8, { align: "right" });

//     doc.moveTo(pageLeft, tableTop + 28).lineTo(pageRight, tableTop + 28).stroke();

//     /* -------- ROWS -------- */
//     doc.font("Helvetica").fontSize(10);

//     let rowY = tableTop + 38;

//     const earnings = employee.earnings || [];
//     const deductions = employee.deductions || [];

//     const maxRows = Math.max(earnings.length, deductions.length, 1);

//     for (let i = 0; i < maxRows; i++) {
//         const e = earnings[i];
//         const d = deductions[i];

//         if (e) {
//             doc.text(e.name, pageLeft + 10, rowY);
//             doc.text(`₹${e.amount}`, colMid - 20, rowY, { align: "right" });
//         }

//         if (d) {
//             doc.text(d.name, colMid + 10, rowY);
//             doc.text(`₹${d.amount}`, pageRight - 20, rowY, { align: "right" });
//         } else if (!deductions.length && i === 0) {
//             doc.text("No deductions", colMid + 10, rowY);
//             doc.text("--", pageRight - 20, rowY, { align: "right" });
//         }

//         rowY += 18;
//     }

//     y += 140;

//     /* ================= NET PAY ================= */
//     //   doc.moveTo(pageLeft, y).lineTo(pageRight, y).stroke();
//     //   y += 20;

//     //   doc
//     //     .font("Helvetica-Bold")
//     //     .fontSize(12)
//     //     .fillColor("green")
//     //     .text(`Net Pay: ₹${employee.netPay}`, pageLeft, y, {
//     //       width: pageRight - pageLeft,
//     //       align: "right",
//     //     })
//     //     .fillColor("black");

//     //   y += 40;
//     doc.moveTo(pageLeft, y).lineTo(pageRight, y).stroke();
//     y += 20;

//     // ---- NET PAY LABEL (BLACK) ----
//     doc
//         .font("Helvetica-Bold")
//         .fontSize(12)
//         .fillColor("black")
//         .text("Net Pay:", pageLeft, y, {
//             continued: true, // 🔥 keeps cursor on same line
//             width: pageRight - pageLeft,
//             align: "right",
//         });

//     // ---- NET PAY AMOUNT (GREEN) ----
//     doc
//         .fillColor("green")
//         .text(` ₹${employee.netPay}`, {
//             continued: false,
//         });

//     // reset color
//     doc.fillColor("black");

//     y += 35;


//     /* ================= FOOTER ================= */
//     doc
//         .font("Helvetica")
//         .fontSize(9)
//         .text(
//             "This is a system generated payslip and does not require a signature.",
//             pageLeft,
//             y
//         );

//     doc.end();
// };

// module.exports = generatePayslipPdf;


// const PDFDocument = require("pdfkit");
// const path = require("path");

// const generatePayslipPdf = (res, data) => {
//   const { company, employee, month, year } = data;

//   const doc = new PDFDocument({ size: "A4", margin: 40 });

//   res.setHeader("Content-Type", "application/pdf");
//   res.setHeader(
//     "Content-Disposition",
//     `attachment; filename=PAYSLIP-${employee.employeeId}-${month}-${year}.pdf`
//   );

//   doc.pipe(res);

//   const pageLeft = 40;
//   const pageRight = 555;
//   let y = 40;

//   /* ================= LOGO ================= */
//   const logoPath = path.resolve(process.cwd(), "uploads/images/company-logo.png");
//   try {
//     doc.image(logoPath, pageLeft, y, { width: 90 });
//     y += 30;
//   } catch {
//     y += 20;
//   }

//   /* ================= COMPANY DETAILS ================= */
//   doc.font("Helvetica-Bold").fontSize(14).text(company.name, pageLeft, y);
//   y += 18;

//   doc.font("Helvetica").fontSize(9).text(company.address, pageLeft, y);
//   y += 14;

//   doc.text(`GSTIN: ${company.gstin || "--"}`, pageLeft, y);
//   y += 18;

//   /* ================= PAYSLIP META (RIGHT) ================= */
//   doc
//     .font("Helvetica-Bold")
//     .fontSize(12)
//     .text("Payslip", pageLeft, 80, {
//       width: pageRight - pageLeft,
//       align: "right",
//     });

//   doc
//     .font("Helvetica")
//     .fontSize(10)
//     .text(
//       new Date(year, month - 1).toLocaleString("en-IN", {
//         month: "long",
//         year: "numeric",
//       }),
//       pageLeft,
//       98,
//       { width: pageRight - pageLeft, align: "right" }
//     );

//   /* ================= EMPLOYEE DETAILS ================= */
//   const boxHeight = 110;
//   const midX = (pageLeft + pageRight) / 2;

//   doc.rect(pageLeft, y, pageRight - pageLeft, boxHeight).stroke();
//   doc.moveTo(midX, y).lineTo(midX, y + boxHeight).stroke();

//   doc.font("Helvetica").fontSize(10);

//   let ly = y + 12;
//   doc.text(`Name: ${employee.name}`, pageLeft + 10, ly);
//   ly += 18;
//   doc.text(`Employee ID: ${employee.employeeId}`, pageLeft + 10, ly);
//   ly += 18;
//   doc.text(`Designation: ${employee.role}`, pageLeft + 10, ly);
//   ly += 18;
//   doc.text(`Department: ${employee.department}`, pageLeft + 10, ly);

//   let ry = y + 12;
//   doc.text(`Bank: ${employee.bankName}`, midX + 10, ry);
//   ry += 18;
//   doc.text(`Account No: ${employee.bankAccount}`, midX + 10, ry);
//   ry += 18;
//   doc.text(`PAN: ${employee.pan}`, midX + 10, ry);
//   ry += 18;
//   doc.text(`PF / UAN: ${employee.pfUan}`, midX + 10, ry);

//   y += boxHeight + 30;

//   /* ================= EARNINGS / DEDUCTIONS ================= */
//   const tableHeight = 150;
//   const colMid = midX;

//   doc.rect(pageLeft, y, pageRight - pageLeft, tableHeight).stroke();
//   doc.moveTo(colMid, y).lineTo(colMid, y + tableHeight).stroke();

//   doc.font("Helvetica-Bold").fontSize(10);
//   doc.text("EARNINGS", pageLeft + 10, y + 8);
//   doc.text("AMOUNT (₹)", colMid - 20, y + 8, { align: "right" });

//   doc.text("DEDUCTIONS", colMid + 10, y + 8);
//   doc.text("AMOUNT (₹)", pageRight - 20, y + 8, { align: "right" });

//   doc.moveTo(pageLeft, y + 28).lineTo(pageRight, y + 28).stroke();

//   let rowY = y + 38;
//   doc.font("Helvetica").fontSize(10);

//   const earnings = employee.earnings || [];
//   const deductions = employee.deductions || [];

//   let totalEarnings = 0;
//   let totalDeductions = 0;

//   const rows = Math.max(earnings.length, deductions.length, 1);

//   for (let i = 0; i < rows; i++) {
//     if (earnings[i]) {
//       totalEarnings += earnings[i].amount;
//       doc.text(earnings[i].name, pageLeft + 10, rowY);
//       doc.text(`₹${earnings[i].amount}`, colMid - 20, rowY, { align: "right" });
//     }

//     if (deductions[i]) {
//       totalDeductions += deductions[i].amount;
//       doc.text(deductions[i].name, colMid + 10, rowY);
//       doc.text(`₹${deductions[i].amount}`, pageRight - 20, rowY, { align: "right" });
//     } else if (!deductions.length && i === 0) {
//       doc.text("No deductions", colMid + 10, rowY);
//       doc.text("--", pageRight - 20, rowY, { align: "right" });
//     }

//     rowY += 18;
//   }

//   /* ===== TOTALS ===== */
//   rowY += 8;
//   doc.font("Helvetica-Bold");
//   doc.text("Total Earnings", pageLeft + 10, rowY);
//   doc.text(`₹${totalEarnings}`, colMid - 20, rowY, { align: "right" });

//   doc.text("Total Deductions", colMid + 10, rowY);
//   doc.text(`₹${totalDeductions}`, pageRight - 20, rowY, { align: "right" });

//   y += tableHeight + 25;

//   /* ================= NET PAY ================= */
//   doc.moveTo(pageLeft, y).lineTo(pageRight, y).stroke();
//   y += 20;

//   doc
//     .font("Helvetica-Bold")
//     .fontSize(12)
//     .fillColor("black")
//     .text("Net Pay:", pageLeft, y, {
//       continued: true,
//       width: pageRight - pageLeft,
//       align: "right",
//     });

//   doc.fillColor("green").text(` ₹${employee.netPay}`);
//   doc.fillColor("black");

//   y += 35;

//   /* ================= FOOTER ================= */
//   doc
//     .font("Helvetica")
//     .fontSize(9)
//     .text(
//       "This is a system generated payslip and does not require a signature.",
//       pageLeft,
//       y
//     );

//   doc.end();
// };

// module.exports = generatePayslipPdf;


// const PDFDocument = require("pdfkit");
// const path = require("path");

// const generatePayslipPdf = (res, data) => {
//     const { company, employee, month, year } = data;

//     const doc = new PDFDocument({ size: "A4", margin: 40 });

//     res.setHeader("Content-Type", "application/pdf");
//     res.setHeader(
//         "Content-Disposition",
//         `attachment; filename=PAYSLIP-${employee.employeeId}-${month}-${year}.pdf`
//     );

//     doc.pipe(res);

//     const left = 40;
//     const right = 555;
//     let y = 40;

//     /* ================= HEADER (LOGO + PAYSLIP SAME ROW) ================= */
//     const logoPath = path.resolve(process.cwd(), "uploads/images/company-logo.png");

//     try {
//         doc.image(logoPath, left, y, { width: 80 });
//     } catch { }

//     doc
//         .font("Helvetica-Bold")
//         .fontSize(12)
//         .text("Payslip", left, y, { width: right - left, align: "right" });

//     doc
//         .font("Helvetica")
//         .fontSize(9)
//         .text(
//             new Date(year, month - 1).toLocaleString("en-IN", {
//                 month: "long",
//                 year: "numeric",
//             }),
//             left,
//             y + 16,
//             { width: right - left, align: "right" }
//         );

//     y += 25;

//     /* ================= COMPANY INFO ================= */
//     doc.font("Helvetica-Bold").fontSize(13).text(company.name, left, y);
//     y += 16;

//     doc.font("Helvetica").fontSize(9);
//     doc.text(company.address || "--", left, y);
//     y += 12;
//     doc.text(`GSTIN: ${company.gstin || "--"}`, left, y);

//     y += 20;
//     // doc.moveTo(left, y).lineTo(right, y).stroke();
//     y += 20;

//     /* ================= EMPLOYEE DETAILS TABLE ================= */
//     const boxH = 95;
//     const mid = (left + right) / 2;

//     doc.rect(left, y, right - left, boxH).stroke();
//     doc.moveTo(mid, y).lineTo(mid, y + boxH).stroke();

//     doc.fontSize(10).font("Helvetica");

//     let ly = y + 12;
//     doc.text(`Name: ${employee.name || "--"}`, left + 10, ly);
//     doc.text(`Employee ID: ${employee.employeeId || "--"}`, left + 10, ly + 18);
//     doc.text(`Designation: ${employee.role || "--"}`, left + 10, ly + 36);
//     doc.text(`Department: ${employee.department || "--"}`, left + 10, ly + 54);

//     let ry = y + 12;
//     doc.text(`Bank: ${employee.bankName || "--"}`, mid + 10, ry);
//     doc.text(`Account No: ${employee.bankAccount || "--"}`, mid + 10, ry + 18);
//     doc.text(`PAN: ${employee.pan || "--"}`, mid + 10, ry + 36);
//     doc.text(`PF / UAN: ${employee.pfUan || "--"}`, mid + 10, ry + 54);

//     y += boxH + 25;
//     const tableY = y;
//     const tableHeight = 90;

//     // Table widths
//     const tableGap = 20;
//     const tableWidth = (right - left - tableGap) / 2;

//     // LEFT TABLE (EARNINGS)
//     const earnX = left;

//     // RIGHT TABLE (DEDUCTIONS)
//     const dedX = earnX + tableWidth + tableGap;

//     const rowH = 26;

//     /* ====== DRAW EARNINGS TABLE ====== */
//     doc.rect(earnX, tableY, tableWidth, tableHeight).stroke();

//     // Column divider
//     doc.moveTo(earnX + tableWidth - 90, tableY)
//         .lineTo(earnX + tableWidth - 90, tableY + tableHeight)
//         .stroke();

//     // Row lines
//     doc.moveTo(earnX, tableY + rowH).lineTo(earnX + tableWidth, tableY + rowH).stroke();
//     doc.moveTo(earnX, tableY + rowH * 2).lineTo(earnX + tableWidth, tableY + rowH * 2).stroke();

//     // Header
//     doc.font("Helvetica-Bold").fontSize(10);
//     doc.text("EARNINGS", earnX + 10, tableY + 8);
//     doc.text("AMOUNT (₹)", earnX + tableWidth - 10, tableY + 8, { align: "right" });

//     // Data
//     doc.font("Helvetica").fontSize(10);
//     doc.text(
//         employee.earnings?.[0]?.name || "Basic Salary",
//         earnX + 10,
//         tableY + rowH + 8
//     );
//     doc.text(
//         `₹${employee.earnings?.[0]?.amount || 0}`,
//         earnX + tableWidth - 10,
//         tableY + rowH + 8,
//         { align: "right" }
//     );

//     // Total
//     const totalEarnings = (employee.earnings || []).reduce(
//         (sum, e) => sum + (e.amount || 0),
//         0
//     );

//     doc.font("Helvetica-Bold");
//     doc.text("Total Earnings", earnX + 10, tableY + rowH * 2 + 8);
//     doc.text(
//         `₹${totalEarnings}`,
//         earnX + tableWidth - 10,
//         tableY + rowH * 2 + 8,
//         { align: "right" }
//     );

//     /* ====== DRAW DEDUCTIONS TABLE ====== */
//     doc.rect(dedX, tableY, tableWidth, tableHeight).stroke();

//     // Column divider
//     doc.moveTo(dedX + tableWidth - 90, tableY)
//         .lineTo(dedX + tableWidth - 90, tableY + tableHeight)
//         .stroke();

//     // Row lines
//     doc.moveTo(dedX, tableY + rowH).lineTo(dedX + tableWidth, tableY + rowH).stroke();
//     doc.moveTo(dedX, tableY + rowH * 2).lineTo(dedX + tableWidth, tableY + rowH * 2).stroke();

//     // Header
//     doc.font("Helvetica-Bold").fontSize(10);
//     doc.text("DEDUCTIONS", dedX + 10, tableY + 8);
//     doc.text("AMOUNT (₹)", dedX + tableWidth - 10, tableY + 8, { align: "right" });

//     // Data
//     doc.font("Helvetica").fontSize(10);

//     if (employee.deductions?.length) {
//         doc.text(employee.deductions[0].name, dedX + 10, tableY + rowH + 8);
//         doc.text(
//             `₹${employee.deductions[0].amount}`,
//             dedX + tableWidth - 10,
//             tableY + rowH + 8,
//             { align: "right" }
//         );
//     } else {
//         doc.text("No deductions", dedX + 10, tableY + rowH + 8);
//         doc.text("--", dedX + tableWidth - 10, tableY + rowH + 8, {
//             align: "right",
//         });
//     }

//     // Total
//     const totalDeductions = (employee.deductions || []).reduce(
//         (sum, d) => sum + (d.amount || 0),
//         0
//     );

//     doc.font("Helvetica-Bold");
//     doc.text("Total Deductions", dedX + 10, tableY + rowH * 2 + 8);
//     doc.text(
//         `₹${totalDeductions}`,
//         dedX + tableWidth - 10,
//         tableY + rowH * 2 + 8,
//         { align: "right" }
//     );

//     y += tableHeight + 30;


//     /* ================= NET PAY ================= */
//     doc.moveTo(left, y).lineTo(right, y).stroke();
//     y += 16;

//     doc
//         .font("Helvetica-Bold")
//         .fontSize(12)
//         .text("Net Pay:", left, y, { continued: true, align: "right", width: right - left });

//     doc.fillColor("green").text(` ₹${employee.netPay}`);
//     doc.fillColor("black");

//     y += 30;

//     /* ================= FOOTER ================= */
//     doc
//         .font("Helvetica")
//         .fontSize(9)
//         .text(
//             "This is a system generated payslip and does not require a signature.",
//             left,
//             y
//         );

//     doc.end();
// };

// module.exports = generatePayslipPdf;


const PDFDocument = require("pdfkit");
const path = require("path");

const generatePayslipPdf = (res, data) => {
    const { company, employee, month, year } = data;

    const doc = new PDFDocument({ size: "A4", margin: 40 });

    res.setHeader("Content-Type", "application/pdf");
    res.setHeader(
        "Content-Disposition",
        `attachment; filename=PAYSLIP-${employee.employeeId}-${month}-${year}.pdf`
    );

    doc.pipe(res);

    const left = 40;
    const right = 555;
    let y = 40;

    /* ================= HEADER ================= */
    const logoPath = path.resolve(process.cwd(), "uploads/images/company-logo.png");

    try {
        doc.image(logoPath, left, y, { width: 80 });
    } catch { }

    doc
        .font("Helvetica-Bold")
        .fontSize(12)
        .text("Payslip", left, y, { width: right - left, align: "right" });

    doc
        .font("Helvetica")
        .fontSize(9)
        .text(
            new Date(year, month - 1).toLocaleString("en-IN", {
                month: "long",
                year: "numeric",
            }),
            left,
            y + 16,
            { width: right - left, align: "right" }
        );

    y += 25;

    /* ================= COMPANY INFO ================= */
    doc.font("Helvetica-Bold").fontSize(13).text(company.name, left, y);
    y += 16;

    doc.font("Helvetica").fontSize(9);
    doc.text(company.address || "--", left, y);
    y += 12;
    doc.text(`GSTIN: ${company.gstin || "--"}`, left, y);

    y += 20;
    y += 20;

    /* ================= EMPLOYEE DETAILS ================= */
    const boxH = 95;
    const mid = (left + right) / 2;

    doc.rect(left, y, right - left, boxH).stroke();
    doc.moveTo(mid, y).lineTo(mid, y + boxH).stroke();

    doc.fontSize(10).font("Helvetica");

    let ly = y + 12;
    doc.text(`Name: ${employee.name || "--"}`, left + 10, ly);
    doc.text(`Employee ID: ${employee.employeeId || "--"}`, left + 10, ly + 18);
    doc.text(`Designation: ${employee.role || "--"}`, left + 10, ly + 36);
    doc.text(`Department: ${employee.department || "--"}`, left + 10, ly + 54);

    let ry = y + 12;
    doc.text(`Bank: ${employee.bankName || "--"}`, mid + 10, ry);
    doc.text(`Account No: ${employee.bankAccount || "--"}`, mid + 10, ry + 18);
    doc.text(`PAN: ${employee.pan || "--"}`, mid + 10, ry + 36);
    doc.text(`PF / UAN: ${employee.pfUan || "--"}`, mid + 10, ry + 54);

    y += boxH + 25;

    /* ================= EARNINGS & DEDUCTIONS ================= */
    const tableY = y;
    const rowH = 26;
    const tableGap = 20;
    const tableWidth = (right - left - tableGap) / 2;
    const amountColWidth = 80;

    const earnX = left;
    const dedX = earnX + tableWidth + tableGap;

    const earnAmountX = earnX + tableWidth - amountColWidth;
    const dedAmountX = dedX + tableWidth - amountColWidth;

    const totalEarnings = (employee.earnings || []).reduce(
        (s, e) => s + (e.amount || 0),
        0
    );
    const totalDeductions = (employee.deductions || []).reduce(
        (s, d) => s + (d.amount || 0),
        0
    );

    /* -------- EARNINGS TABLE -------- */
    doc.rect(earnX, tableY, tableWidth, rowH * 3).stroke();
    doc.moveTo(earnAmountX, tableY).lineTo(earnAmountX, tableY + rowH * 3).stroke();
    doc.moveTo(earnX, tableY + rowH).lineTo(earnX + tableWidth, tableY + rowH).stroke();
    doc.moveTo(earnX, tableY + rowH * 2).lineTo(earnX + tableWidth, tableY + rowH * 2).stroke();

    doc.font("Helvetica-Bold").fontSize(10);
    doc.text("EARNINGS", earnX + 10, tableY + 8);
    doc.text("AMOUNT (₹)", earnAmountX, tableY + 8, {
        width: amountColWidth - 10,
        align: "right",
    });

    doc.font("Helvetica").fontSize(10);
    doc.text("Basic Salary", earnX + 10, tableY + rowH + 8);
    doc.text(`₹${employee.earnings?.[0]?.amount || 0}`, earnAmountX, tableY + rowH + 8, {
        width: amountColWidth - 10,
        align: "right",
    });

    doc.font("Helvetica-Bold");
    doc.text("Total Earnings", earnX + 10, tableY + rowH * 2 + 8);
    doc.text(`₹${totalEarnings}`, earnAmountX, tableY + rowH * 2 + 8, {
        width: amountColWidth - 10,
        align: "right",
    });

    /* -------- DEDUCTIONS TABLE -------- */
    doc.rect(dedX, tableY, tableWidth, rowH * 3).stroke();
    doc.moveTo(dedAmountX, tableY).lineTo(dedAmountX, tableY + rowH * 3).stroke();
    doc.moveTo(dedX, tableY + rowH).lineTo(dedX + tableWidth, tableY + rowH).stroke();
    doc.moveTo(dedX, tableY + rowH * 2).lineTo(dedX + tableWidth, tableY + rowH * 2).stroke();

    doc.font("Helvetica-Bold").fontSize(10);
    doc.text("DEDUCTIONS", dedX + 10, tableY + 8);
    doc.text("AMOUNT (₹)", dedAmountX, tableY + 8, {
        width: amountColWidth - 10,
        align: "right",
    });

    doc.font("Helvetica").fontSize(10);
    doc.text(
        employee.deductions?.length ? employee.deductions[0].name : "No deductions",
        dedX + 10,
        tableY + rowH + 8
    );
    doc.text(
        employee.deductions?.length ? `₹${employee.deductions[0].amount}` : "--",
        dedAmountX,
        tableY + rowH + 8,
        {
            width: amountColWidth - 10,
            align: "right",
        }
    );

    doc.font("Helvetica-Bold");
    doc.text("Total Deductions", dedX + 10, tableY + rowH * 2 + 8);
    doc.text(`₹${totalDeductions}`, dedAmountX, tableY + rowH * 2 + 8, {
        width: amountColWidth - 10,
        align: "right",
    });

    y += rowH * 3 + 30;

    /* ================= NET PAY ================= */
    doc.moveTo(left, y).lineTo(right, y).stroke();
    y += 16;

    //   doc
    //     .font("Helvetica-Bold")
    //     .fontSize(12)
    //     .text("Net Pay:", left, y, {
    //       width: right - left,
    //       align: "right",
    //       continued: true,
    //     });

    //   doc.fillColor("green").text(` ₹${employee.netPay}`);
    //   doc.fillColor("black");
    // Right-align whole Net Pay block
    doc
        .font("Helvetica-Bold")
        .fontSize(12)
        .fillColor("black")
        .text("Net Pay:", right - 80, y, {
            continued: true,
        });

    // Amount with proper gap
    doc
        .fillColor("green")
        .text(` ₹${employee.netPay}`);

    // reset
    doc.fillColor("black");


    y += 30;

    /* ================= FOOTER ================= */
    doc
        .font("Helvetica")
        .fontSize(9)
        .text(
            "This is a system generated payslip and does not require a signature.",
            left,
            y
        );

    doc.end();
};

module.exports = generatePayslipPdf;
