import { jsPDF } from 'jspdf';

/**
 * Constants for report layout configuration
 * Using Points (pt) where 72pt = 1 inch
 * Letter Size: 612pt x 792pt
 */
export const REPORT_LAYOUT_STRUCTURE = {
    PAGE_WIDTH: 612,
    PAGE_HEIGHT: 792,
    MARGIN: 40,
    CONTENT_WIDTH: 532, // 612 - 40 - 40
    HEADER_HEIGHT: 60,
    FOOTER_HEIGHT: 40,
};

/**
 * Helper to calculate page numbers for a list of sections.
 */
export const calculateTOCPageNumbers = (sections, startPage = 3) => {
    let currentPage = startPage;
    return sections.map(section => {
        const entry = {
            title: section.title,
            pageNumber: currentPage
        };
        const estimatedPages = Math.max(1, Math.ceil((section.data?.length || 0) / 20)); 
        currentPage += estimatedPages;
        return entry;
    });
};

/**
 * Standardizes styling for a section header in jsPDF
 */
export const drawSectionHeader = (doc, title, yPosition) => {
    doc.setFont('helvetica', 'bold');
    doc.setFontSize(18);
    doc.setTextColor(15, 23, 42); // Slate 900
    doc.text(title.toUpperCase(), REPORT_LAYOUT_STRUCTURE.MARGIN, yPosition);
    
    // Underline
    doc.setDrawColor(30, 41, 59);
    doc.setLineWidth(2);
    doc.line(REPORT_LAYOUT_STRUCTURE.MARGIN, yPosition + 8, REPORT_LAYOUT_STRUCTURE.PAGE_WIDTH - REPORT_LAYOUT_STRUCTURE.MARGIN, yPosition + 8);

    return yPosition + 30;
};

/**
 * Adds a standard footer to the current page
 */
export const drawPageFooter = (doc, pageNumber, text = "Management Report") => {
    const y = REPORT_LAYOUT_STRUCTURE.PAGE_HEIGHT - 20;
    doc.setFont('helvetica', 'normal');
    doc.setFontSize(8);
    doc.setTextColor(148, 163, 184); // Slate 400
    
    doc.text(text.toUpperCase(), REPORT_LAYOUT_STRUCTURE.MARGIN, y);
    doc.text(`PAGE ${pageNumber}`, REPORT_LAYOUT_STRUCTURE.PAGE_WIDTH - REPORT_LAYOUT_STRUCTURE.MARGIN, y, { align: 'right' });
};
