const { execSync } = require('child_process');
const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch({
        executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
        headless: true,
        args: ['--no-sandbox', '--disable-setuid-sandbox']
    });

    const page = await browser.newPage();
    await page.setViewport({ width: 1240, height: 1754 });

    // Load page
    await page.goto('http://127.0.0.1:8889/cotizacion.html', {
        waitUntil: 'networkidle0',
        timeout: 30000
    });

    // Dismiss modal and make all animated elements visible
    await page.evaluate(() => {
        // Close modal
        var modal = document.getElementById('confidentialModal');
        if (modal) { modal.style.display = 'none'; modal.classList.add('hidden'); }
        document.body.style.overflow = '';

        // Force all animated elements to be visible
        var animated = document.querySelectorAll('.animate-on-scroll');
        animated.forEach(function(el) {
            el.style.opacity = '1';
            el.style.transform = 'none';
        });

        // Hide the fixed navbar (not needed in PDF)
        var navbar = document.querySelector('.navbar');
        if (navbar) navbar.style.display = 'none';
    });

    // Wait for fonts and images
    await new Promise(r => setTimeout(r, 1500));

    const pdfBuffer = await page.pdf({
        path: '/Users/novatech/Cotizaciones/BUENAGUI/cotizacion-buenagui.pdf',
        format: 'A4',
        printBackground: true,
        margin: { top: '0', right: '0', bottom: '0', left: '0' }
    });

    await browser.close();
    console.log('PDF generated successfully:', pdfBuffer.length, 'bytes');
})().catch(err => {
    console.error('Error:', err.message);
    process.exit(1);
});
