#!/usr/bin/env python3
"""Generate beautiful PDF manuals for HEFESTO from Markdown sources."""

import markdown
import subprocess
import os
import tempfile

MANUALS = [
    {
        "md": "MANUAL-TECNICO.md",
        "pdf": "MANUAL-TECNICO-v2.pdf",
        "title": "Manual Técnico",
        "subtitle": "Documentação Técnica Completa",
        "version": "v2.0",
        "type": "technical"
    },
    {
        "md": "MANUAL-NEGOCIOS.md",
        "pdf": "MANUAL-NEGOCIOS-v2.pdf",
        "title": "Manual de Negócios",
        "subtitle": "Visão Comercial e Estratégica",
        "version": "v2.0",
        "type": "business"
    }
]

CSS = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap');

@page {
    size: A4;
    margin: 2cm 2cm 2.5cm 2cm;
    @top-center {
        content: "";
    }
    @bottom-center {
        content: counter(page);
        font-family: 'Inter', 'Segoe UI', sans-serif;
        font-size: 9pt;
        color: #999;
    }
}

@page :first {
    margin: 0;
    @bottom-center { content: ""; }
}

* { box-sizing: border-box; }

body {
    font-family: 'Inter', 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
    font-size: 10.5pt;
    line-height: 1.7;
    color: #333;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
}

/* COVER PAGE */
.cover {
    page-break-after: always;
    width: 210mm;
    height: 297mm;
    margin: -2cm;
    padding: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: linear-gradient(160deg, #0D1B2A 0%, #1A237E 40%, #1A237E 60%, #0D1B2A 100%);
    color: white;
    text-align: center;
    position: relative;
    overflow: hidden;
}

.cover::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 8px;
    background: linear-gradient(90deg, #E65100, #FF8F00, #FFB300);
}

.cover::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 8px;
    background: linear-gradient(90deg, #FFB300, #FF8F00, #E65100);
}

.cover .logo-icon {
    font-size: 80pt;
    margin-bottom: 10px;
    filter: drop-shadow(0 4px 20px rgba(255, 143, 0, 0.5));
}

.cover .brand {
    font-size: 42pt;
    font-weight: 900;
    letter-spacing: 12px;
    margin-bottom: 5px;
    background: linear-gradient(90deg, #FF8F00, #FFB300);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.cover .divider {
    width: 120px;
    height: 3px;
    background: linear-gradient(90deg, transparent, #FF8F00, transparent);
    margin: 20px auto;
}

.cover .manual-title {
    font-size: 22pt;
    font-weight: 300;
    letter-spacing: 3px;
    text-transform: uppercase;
    color: rgba(255,255,255,0.95);
    margin-bottom: 8px;
}

.cover .manual-subtitle {
    font-size: 12pt;
    font-weight: 300;
    color: rgba(255,255,255,0.6);
    margin-bottom: 60px;
}

.cover .meta {
    position: absolute;
    bottom: 50px;
    text-align: center;
    width: 100%;
}

.cover .company {
    font-size: 11pt;
    font-weight: 500;
    letter-spacing: 4px;
    text-transform: uppercase;
    color: rgba(255,255,255,0.5);
    margin-bottom: 8px;
}

.cover .date {
    font-size: 10pt;
    color: rgba(255,255,255,0.35);
    letter-spacing: 2px;
}

/* GEOMETRIC DECORATIONS */
.cover .geo1 {
    position: absolute;
    top: 60px;
    right: 60px;
    width: 200px;
    height: 200px;
    border: 1px solid rgba(255,143,0,0.15);
    border-radius: 50%;
}

.cover .geo2 {
    position: absolute;
    bottom: 120px;
    left: 40px;
    width: 150px;
    height: 150px;
    border: 1px solid rgba(255,143,0,0.1);
    transform: rotate(45deg);
}

/* PAGE HEADER */
.page-header {
    page-break-after: avoid;
    margin-bottom: 30px;
    padding-bottom: 12px;
    border-bottom: 2px solid #E65100;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 9pt;
    color: #999;
    letter-spacing: 2px;
    text-transform: uppercase;
}

.page-header .left {
    font-weight: 700;
    color: #1A237E;
}

.page-header .right {
    color: #E65100;
}

/* TABLE OF CONTENTS */
.toc-page {
    page-break-after: always;
}

.toc-page h2 {
    font-size: 18pt;
    color: #1A237E;
    border: none;
    padding: 0;
    margin-bottom: 25px;
    letter-spacing: 3px;
    text-transform: uppercase;
}

.toc-page h2::before {
    content: '';
    display: block;
    width: 50px;
    height: 3px;
    background: #E65100;
    margin-bottom: 15px;
}

/* HEADINGS */
h1 {
    font-size: 22pt;
    font-weight: 800;
    color: #1A237E;
    margin-top: 40px;
    margin-bottom: 20px;
    padding-bottom: 10px;
    border-bottom: 3px solid #E65100;
    page-break-after: avoid;
    letter-spacing: -0.5px;
}

h2 {
    font-size: 16pt;
    font-weight: 700;
    color: #1A237E;
    margin-top: 35px;
    margin-bottom: 15px;
    padding-left: 15px;
    border-left: 4px solid #E65100;
    page-break-after: avoid;
}

h3 {
    font-size: 13pt;
    font-weight: 600;
    color: #283593;
    margin-top: 25px;
    margin-bottom: 12px;
    page-break-after: avoid;
}

h4 {
    font-size: 11pt;
    font-weight: 600;
    color: #E65100;
    margin-top: 20px;
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 1px;
}

/* FIRST H1 — remove top margin after cover */
.content > h1:first-child {
    margin-top: 0;
}

/* PARAGRAPHS */
p {
    margin-bottom: 12px;
    text-align: justify;
    hyphens: auto;
}

/* STRONG/BOLD in special contexts */
strong {
    color: #1A237E;
    font-weight: 600;
}

/* TABLES */
table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
    font-size: 9.5pt;
    page-break-inside: avoid;
    box-shadow: 0 1px 4px rgba(0,0,0,0.08);
    border-radius: 6px;
    overflow: hidden;
}

thead {
    background: linear-gradient(135deg, #1A237E, #283593);
}

th {
    color: white;
    font-weight: 600;
    text-align: left;
    padding: 12px 14px;
    font-size: 9pt;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

td {
    padding: 10px 14px;
    border-bottom: 1px solid #E8EAF6;
    vertical-align: top;
}

tbody tr:nth-child(even) {
    background-color: #F5F5FF;
}

tbody tr:hover {
    background-color: #E8EAF6;
}

/* Checkmark styling for comparison tables */
td:has(text("✅")), td:has(text("❌")) {
    text-align: center;
    font-size: 14pt;
}

/* CODE BLOCKS */
pre {
    background: #1E1E2E;
    color: #CDD6F4;
    border-radius: 8px;
    padding: 18px 20px;
    font-size: 9pt;
    line-height: 1.6;
    overflow-x: auto;
    margin: 18px 0;
    page-break-inside: avoid;
    border-left: 4px solid #E65100;
}

code {
    font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 9pt;
}

p code, li code, td code {
    background: #EDE7F6;
    color: #4A148C;
    padding: 2px 7px;
    border-radius: 4px;
    font-size: 8.5pt;
}

/* LISTS */
ul, ol {
    margin: 10px 0;
    padding-left: 24px;
}

li {
    margin-bottom: 6px;
    line-height: 1.6;
}

li::marker {
    color: #E65100;
    font-weight: bold;
}

/* BLOCKQUOTES / CALLOUTS */
blockquote {
    background: linear-gradient(135deg, #FFF3E0, #FFF8E1);
    border-left: 4px solid #FF8F00;
    margin: 20px 0;
    padding: 16px 20px;
    border-radius: 0 8px 8px 0;
    font-style: normal;
    page-break-inside: avoid;
}

blockquote p {
    margin: 0;
    color: #5D4037;
}

blockquote strong {
    color: #E65100;
}

/* HORIZONTAL RULES */
hr {
    border: none;
    height: 2px;
    background: linear-gradient(90deg, #E65100, #FF8F00, transparent);
    margin: 35px 0;
}

/* LINKS */
a {
    color: #1A237E;
    text-decoration: none;
    border-bottom: 1px solid #E65100;
}

/* INFO BOX */
.info-box {
    background: #E3F2FD;
    border-left: 4px solid #1565C0;
    padding: 14px 18px;
    border-radius: 0 8px 8px 0;
    margin: 18px 0;
}

/* SUCCESS BOX */
.success-box {
    background: #E8F5E9;
    border-left: 4px solid #2E7D32;
    padding: 14px 18px;
    border-radius: 0 8px 8px 0;
    margin: 18px 0;
}

/* WARNING BOX */
.warning-box {
    background: #FFF3E0;
    border-left: 4px solid #E65100;
    padding: 14px 18px;
    border-radius: 0 8px 8px 0;
    margin: 18px 0;
}

/* PAGE BREAKS for major sections */
h1 {
    page-break-before: always;
}

h1:first-of-type {
    page-break-before: avoid;
}

/* FOOTER NOTE */
.doc-footer {
    margin-top: 40px;
    padding-top: 15px;
    border-top: 1px solid #E0E0E0;
    text-align: center;
    font-size: 8.5pt;
    color: #999;
    font-style: italic;
}

/* Emoji sizing */
.emoji-icon {
    font-size: 14pt;
}

/* Print optimizations */
@media print {
    body { -webkit-print-color-adjust: exact !important; }
    .cover { page-break-after: always; }
    h1, h2, h3 { page-break-after: avoid; }
    table, pre, blockquote { page-break-inside: avoid; }
}
"""

COVER_HTML = """
<div class="cover">
    <div class="geo1"></div>
    <div class="geo2"></div>
    <div class="logo-icon">🔥</div>
    <div class="brand">HEFESTO</div>
    <div class="divider"></div>
    <div class="manual-title">{title}</div>
    <div class="manual-subtitle">{subtitle}</div>
    <div class="meta">
        <div class="company">Kislanski Industries &nbsp;|&nbsp; AI Vertice</div>
        <div class="date">Fevereiro 2026 &nbsp;·&nbsp; {version}</div>
    </div>
</div>
"""

def generate_html(md_content, manual_info):
    extensions = ['tables', 'fenced_code', 'toc', 'nl2br', 'sane_lists']
    ext_configs = {
        'toc': {'title': '', 'toc_depth': '1-3'}
    }
    
    md = markdown.Markdown(extensions=extensions, extension_configs=ext_configs)
    html_body = md.convert(md_content)
    toc_html = md.toc
    
    cover = COVER_HTML.format(
        title=manual_info['title'],
        subtitle=manual_info['subtitle'],
        version=manual_info['version']
    )
    
    header_title = f"HEFESTO — {manual_info['title']}"
    
    full_html = f"""<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HEFESTO - {manual_info['title']}</title>
    <style>{CSS}</style>
</head>
<body>
    {cover}
    
    <div class="toc-page">
        <h2>Sumário</h2>
        {toc_html}
    </div>
    
    <div class="page-header">
        <span class="left">{header_title}</span>
        <span class="right">{manual_info['version']}</span>
    </div>
    
    <div class="content">
        {html_body}
    </div>
    
    <div class="doc-footer">
        HEFESTO {manual_info['version']} &nbsp;·&nbsp; Kislanski Industries | AI Vertice &nbsp;·&nbsp; Fevereiro 2026<br>
        Documento confidencial — Todos os direitos reservados
    </div>
</body>
</html>"""
    
    return full_html


def generate_pdf(html_path, pdf_path):
    cmd = [
        'google-chrome',
        '--headless',
        '--disable-gpu',
        '--no-sandbox',
        '--disable-software-rasterizer',
        f'--print-to-pdf={pdf_path}',
        '--print-to-pdf-no-header',
        '--no-margins',
        f'file://{html_path}'
    ]
    
    result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
    if result.returncode != 0:
        # Try chromium as fallback
        cmd[0] = 'chromium-browser'
        result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
    
    return os.path.exists(pdf_path)


def main():
    docs_dir = os.path.dirname(os.path.abspath(__file__))
    
    for manual in MANUALS:
        md_path = os.path.join(docs_dir, manual['md'])
        pdf_path = os.path.join(docs_dir, manual['pdf'])
        
        print(f"\n{'='*60}")
        print(f"Generating: {manual['title']}")
        print(f"{'='*60}")
        
        # Read markdown
        with open(md_path, 'r', encoding='utf-8') as f:
            md_content = f.read()
        
        # Generate HTML
        html_content = generate_html(md_content, manual)
        
        # Write temp HTML
        html_path = os.path.join(docs_dir, manual['md'].replace('.md', '.html'))
        with open(html_path, 'w', encoding='utf-8') as f:
            f.write(html_content)
        print(f"  ✓ HTML generated: {html_path}")
        
        # Generate PDF
        if generate_pdf(html_path, pdf_path):
            size_mb = os.path.getsize(pdf_path) / (1024*1024)
            print(f"  ✓ PDF generated: {pdf_path} ({size_mb:.1f} MB)")
        else:
            print(f"  ✗ PDF generation failed!")
        
        # Cleanup HTML
        # os.remove(html_path)
    
    print(f"\n{'='*60}")
    print("Done!")


if __name__ == '__main__':
    main()
