import { jsPDF } from 'jspdf';

/**
 * Constants for Management Report layout configuration
 * Using Points (pt) for PDF where 72pt = 1 inch
 * Using Pixels (px) for HTML where 96px = 1 inch
 * Letter Size: 8.5" x 11"
 */
export const REPORT_LAYOUT = {
    // PDF Dimensions (Points)
    PDF_PAGE_WIDTH: 612,
    PDF_PAGE_HEIGHT: 792,
    PDF_MARGIN: 40,
    
    // HTML Dimensions (Pixels @ 96 DPI)
    HTML_PAGE_WIDTH: 816,
    HTML_PAGE_HEIGHT: 1056,
    HTML_MARGIN: 64, // p-16 equivalent

    // Styling Constants
    COLORS: {
        PRIMARY: '#1e293b', // slate-800
        SECONDARY: '#64748b', // slate-500
        ACCENT: '#3b82f6', // blue-500
        BORDER: '#e2e8f0', // slate-200
        BG_ODD: '#f8fafc', // slate-50
    }
};

/**
 * Helper to calculate page numbers for a list of sections.
 * Approximates page counts based on data length.
 * Cover (1) + TOC (1) + Visual Recap (1) + Exec Summary (1) = Start body at 5
 */
export const calculateManagementReportPageNumbers = (sections, startPage = 5) => {
    let currentPage = startPage;
    return sections.map(section => {
        const entry = {
            title: section.title,
            pageNumber: currentPage
        };
        
        const itemsPerPage = 10; 
        const itemCount = Array.isArray(section.data) ? section.data.length : 0;
        
        let finalItemCount = itemCount;
        if (section.type === 'collections_detailed' && section.data?.open) {
            finalItemCount = (section.data.open.length || 0) + (section.data.resolved?.length || 0);
        }

        const estimatedPages = Math.max(1, Math.ceil(finalItemCount / itemsPerPage)); 
        currentPage += estimatedPages;
        return entry;
    });
};

/**
 * Draw a standardized footer on a jsPDF document page
 */
export const drawManagementReportFooter = (doc, pageNumber, clientName = "Management Report") => {
    const pageWidth = doc.internal.pageSize.getWidth();
    const pageHeight = doc.internal.pageSize.getHeight();
    const margin = REPORT_LAYOUT.PDF_MARGIN;
    const y = pageHeight - 20;

    doc.setFont('helvetica', 'normal');
    doc.setFontSize(8);
    doc.setTextColor(148, 163, 184); // slate-400
    
    // Left text
    doc.text(clientName.toUpperCase(), margin, y);
    
    // Right page number
    doc.text(`PAGE ${pageNumber}`, pageWidth - margin, y, { align: 'right' });
    
    // Top line for footer
    doc.setDrawColor(226, 232, 240); // slate-200
    doc.setLineWidth(0.5);
    doc.line(margin, y - 10, pageWidth - margin, y - 10);
};

/**
 * Draw a standardized section header in jsPDF
 */
export const drawManagementReportHeader = (doc, title, yPosition) => {
    const pageWidth = doc.internal.pageSize.getWidth();
    const margin = REPORT_LAYOUT.PDF_MARGIN;

    doc.setFont('helvetica', 'bold');
    doc.setFontSize(18);
    doc.setTextColor(30, 41, 59); // slate-800
    doc.text(title.toUpperCase(), margin, yPosition);
    
    // Underline
    doc.setDrawColor(30, 41, 59);
    doc.setLineWidth(2);
    doc.line(margin, yPosition + 8, pageWidth - margin, yPosition + 8);

    return yPosition + 30; // Return next Y position
};
