from weasyprint import HTML
import re

with open('/opt/mia/workspace/clientes/climb_digital_instagram/copies_13posts.md', 'r') as f:
    raw = f.read()

# Split into posts
posts = re.split(r'\n---\n---\n', raw)

def md_to_html(text):
    text = re.sub(r'^# (.+)$', r'<h1>\1</h1>', text, flags=re.MULTILINE)
    text = re.sub(r'^## (.+)$', r'<h2>\1</h2>', text, flags=re.MULTILINE)
    text = re.sub(r'^### (.+)$', r'<h3>\1</h3>', text, flags=re.MULTILINE)
    text = re.sub(r'\*\*(.+?)\*\*', r'<strong>\1</strong>', text)
    text = re.sub(r'\*(.+?)\*', r'<em>\1</em>', text)
    text = re.sub(r'^- (.+)$', r'<li>\1</li>', text, flags=re.MULTILINE)
    text = re.sub(r'(<li>.*</li>\n?)+', lambda m: '<ul>' + m.group(0) + '</ul>', text, flags=re.DOTALL)
    text = re.sub(r'^\d+\. (.+)$', r'<li>\1</li>', text, flags=re.MULTILINE)
    text = re.sub(r'^---$', r'<hr>', text, flags=re.MULTILINE)
    lines = text.split('\n')
    result = []
    for line in lines:
        stripped = line.strip()
        if stripped and not stripped.startswith('<') and not stripped == '<hr>':
            result.append(f'<p>{stripped}</p>')
        else:
            result.append(line)
    return '\n'.join(result)

posts_html = ''
for i, post in enumerate(posts):
    post = post.strip()
    if not post or 'FIM DAS COPIES' in post:
        continue
    # Detect post header
    header_match = re.search(r'## (POST \d+ .+)', post)
    subheader_match = re.search(r'### "(.+)"', post)

    if header_match:
        header = header_match.group(1)
    else:
        header = f'Post {i+1}'
    subheader = subheader_match.group(1) if subheader_match else ''

    # Separate content blocks by ---
    blocks = re.split(r'\n---\n', post)
    content_html = ''
    for block in blocks:
        block = block.strip()
        if not block:
            continue
        if block.startswith('**SLIDE') or block.startswith('**DURACAO') or block.startswith('**['):
            content_html += f'<div class="slide-block">{md_to_html(block)}</div>'
        elif block.startswith('**Legenda'):
            content_html += f'<div class="legenda-block">{md_to_html(block)}</div>'
        elif '## POST' in block or '### "' in block:
            continue
        else:
            content_html += f'<div class="content-block">{md_to_html(block)}</div>'

    posts_html += f'''
<div class="post-card">
  <div class="post-header">{header}</div>
  <div class="post-subheader">"{subheader}"</div>
  {content_html}
</div>
'''

html = f"""<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<style>
@page {{ size: A4; margin: 15mm 15mm; }}
body {{ font-family: Arial, sans-serif; font-size: 11px; color: #222; line-height: 1.65; }}
.cover {{ text-align: center; padding: 60px 20px; }}
.cover h1 {{ font-size: 24px; color: #0F2A44; margin-bottom: 8px; }}
.cover h2 {{ font-size: 15px; color: #B08D57; font-weight: normal; margin-bottom: 4px; }}
.cover p {{ font-size: 12px; color: #666; }}
.post-card {{ break-before: page; border-top: 4px solid #0F2A44; padding-top: 12px; margin-bottom: 20px; }}
.post-header {{ background: #0F2A44; color: #B08D57; font-size: 13px; font-weight: bold; letter-spacing: 1px; padding: 6px 10px; border-radius: 4px; display: inline-block; margin-bottom: 6px; }}
.post-subheader {{ font-size: 14px; font-weight: bold; color: #1F4F3A; margin-bottom: 12px; font-style: italic; }}
.slide-block {{ background: #f5f5f5; border-left: 3px solid #0F2A44; padding: 8px 12px; margin: 8px 0; border-radius: 0 4px 4px 0; }}
.content-block {{ margin: 8px 0; }}
.legenda-block {{ background: #fff8e1; border: 1px solid #B08D57; border-radius: 6px; padding: 10px 14px; margin-top: 12px; }}
h1 {{ font-size: 16px; color: #0F2A44; margin: 8px 0 4px 0; }}
h2 {{ font-size: 13px; color: #0F2A44; margin: 8px 0 4px 0; }}
h3 {{ font-size: 12px; color: #1F4F3A; margin: 6px 0 3px 0; }}
p {{ margin: 4px 0; }}
ul {{ margin: 4px 0 8px 18px; }}
li {{ margin-bottom: 3px; }}
hr {{ border: none; border-top: 1px solid #ddd; margin: 10px 0; }}
em {{ color: #666; font-size: 10px; }}
strong {{ color: #0F2A44; }}
</style>
</head>
<body>

<div class="cover">
  <h1>Copies - 13 Posts</h1>
  <h2>@oremoraes.ia | Agencia Climb Digital</h2>
  <p>Redacao: Jonathan | Maio 2026</p>
  <p style="margin-top:20px; font-size:11px; color:#888">4 semanas de conteudo | 8 carrosseis + 5 reels</p>
</div>

{posts_html}

</body>
</html>"""

HTML(string=html).write_pdf('/opt/mia/workspace/clientes/climb_digital_instagram/Copies_13Posts_oremoraes.pdf')
print("PDF gerado com sucesso")
