/**
 * Configuration for html2canvas to ensure high-quality rendering
 * suitable for print-ready PDFs.
 */

// Define a robust font stack for Unicode support (Checkmarks, Emojis, Symbols)
export const UNICODE_FONT_STACK = [
  '-apple-system', 
  'BlinkMacSystemFont', 
  '"Segoe UI"', 
  '"Segoe UI Symbol"', // Windows Symbols
  '"Apple Color Emoji"', // macOS Emojis
  '"Noto Color Emoji"', // Linux/Android Emojis
  '"Arial Unicode MS"', // Legacy Windows
  '"DejaVu Sans"', // Linux
  '"Symbola"', // General Symbol font
  'sans-serif'
].join(', ');

export const HIGH_QUALITY_CANVAS_CONFIG = {
  scale: 3, // 3x scale for high resolution (print quality)
  useCORS: true, // Allow loading cross-origin images
  allowTaint: true, // Allow tainting for local images
  backgroundColor: '#ffffff', // Ensure white background
  logging: false, // Disable verbose logging in production
  imageTimeout: 20000, // Longer timeout for high-res images
  removeContainer: true, // Clean up the DOM
  letterRendering: true, // Better text rendering
  width: 816, // Letter width in pixels at 96 DPI
  height: 1056, // Letter height in pixels at 96 DPI
  windowWidth: 816,
  windowHeight: 1056,
  onclone: (clonedDoc) => {
    const element = clonedDoc.body.firstElementChild;
    if (element) {
        // Enforce font smoothing for crisp text
        element.style.fontSmoothing = 'antialiased';
        element.style.webkitFontSmoothing = 'antialiased';
        element.style.mozOsxFontSmoothing = 'grayscale';
        
        // Enforce the Unicode font stack explicitly on the root element being rendered
        element.style.fontFamily = UNICODE_FONT_STACK;
        
        // Ensure symbols have the font applied specifically
        const symbols = element.querySelectorAll('.symbol-char, .check-mark, .status-icon');
        symbols.forEach(el => {
            el.style.fontFamily = UNICODE_FONT_STACK;
        });
    }
  }
};

/**
 * Helper to get config with overrides
 */
export const getCanvasConfig = (overrides = {}) => {
  return {
    ...HIGH_QUALITY_CANVAS_CONFIG,
    ...overrides
  };
};