#!/usr/bin/env python3
"""Generate PDF from Moon ERP presentation HTML."""
import re

# Read the original HTML
with open('/home/moonui/public_html/presentation/index.html', 'r', encoding='utf-8') as f:
    html = f.read()

# 1. Remove navigation, progress bar, counter, keyboard hint, JS
html = re.sub(r'<div class="progress-bar".*?</div>', '', html)
html = re.sub(r'<div class="slide-counter".*?</div>', '', html)
html = re.sub(r'<!-- Navigation -->.*?</div>\s*</div>', '', html, flags=re.DOTALL)
html = re.sub(r'<div class="keyboard-hint">.*?</div>', '', html)
html = re.sub(r'<script>.*?</script>', '', html, flags=re.DOTALL)

# 2. Make all slides visible (remove opacity:0, position:absolute, etc.)
# Replace slide CSS to make them block-level, visible, page-break
pdf_slide_css = """
        @page {
            size: 1280px 720px;
            margin: 0;
        }

        html, body {
            width: 1280px;
            overflow: visible;
            height: auto;
        }

        .deck {
            width: 1280px;
            height: auto;
            position: relative;
        }

        .slide {
            position: relative;
            inset: auto;
            width: 1280px;
            height: 720px;
            display: flex;
            align-items: center;
            justify-content: center;
            opacity: 1 !important;
            pointer-events: auto;
            overflow: hidden;
            page-break-after: always;
            page-break-inside: avoid;
        }

        /* Remove all animations for PDF */
        .reveal {
            opacity: 1 !important;
            transform: none !important;
            animation: none !important;
        }

        .particle { display: none; }
        .moon-icon .crescent { animation: none !important; }

        /* Ensure backgrounds render */
        .slide, .bg-a, .bg-b, .bg-c, .bg-d, .slide-cover {
            -webkit-print-color-adjust: exact;
            print-color-adjust: exact;
            color-adjust: exact;
        }

        /* Gold gradient text needs solid fallback for PDF */
        .highlight, .cover-title, .stat-number, .ib-num {
            -webkit-text-fill-color: #d4a843 !important;
            color: #d4a843 !important;
        }

        .stat-card::before { display: none; }
        .feat-card:hover, .module-card:hover, .info-box:hover,
        .stat-card:hover, .flow-step:hover .flow-circle { transform: none; }

        /* Hide nav-related CSS */
        .nav-controls, .nav-btn, .nav-dots, .nav-dot,
        .slide-counter, .progress-bar, .keyboard-hint { display: none !important; }
"""

# Insert PDF-specific CSS before closing </style>
html = html.replace('    </style>', pdf_slide_css + '\n    </style>')

# Remove .slide.active rules (all slides are visible now)
html = html.replace('.slide.active { opacity: 1; pointer-events: auto; }', '')
html = html.replace('.slide.active .reveal { animation: revealUp 0.9s cubic-bezier(0.16, 1, 0.3, 1) forwards; }', '')

# Remove the "active" class requirement
for i in range(1, 7):
    old = '.slide.active .reveal-delay-' + str(i) + ' { animation-delay:'
    new = '/* removed */ .x-noop { animation-delay:'
    html = html.replace(old, new)

# Write PDF HTML
pdf_html_path = '/home/moonui/public_html/presentation/pdf_version.html'
with open(pdf_html_path, 'w', encoding='utf-8') as f:
    f.write(html)

# Generate PDF using weasyprint
from weasyprint import HTML
print("Generating PDF...")
HTML(filename=pdf_html_path).write_pdf(
    '/home/moonui/public_html/presentation/Moon-ERP-Presentation.pdf'
)
print("Done! PDF saved to: /home/moonui/public_html/presentation/Moon-ERP-Presentation.pdf")
