#!/usr/bin/env python3
import re
import os

slides_dir = 'slides'

# Règles : wrapper les textes dans divs qui ne sont pas déjà dans des balises
def wrap_text_in_divs(content):
    # Pattern pour trouver du texte nu dans des divs
    # Cherche : <div...>TEXTE</div> où TEXTE n'est pas déjà dans <p>, <h>, <ul>, etc.

    lines = content.split('\n')
    result = []

    for line in lines:
        # Si la ligne contient "date" avec emoji
        if '📅' in line and '<div class="date">' in line:
            line = line.replace('<div class="date">📅', '<div class="date"><p>📅')
            line = line.replace('</div>', '</p></div>', 1)

        # Si ligne contient role-title
        if '<div class="role-title">' in line and not '<p>' in line:
            match = re.search(r'<div class="role-title">([^<]+)</div>', line)
            if match:
                text = match.group(1)
                line = line.replace(f'<div class="role-title">{text}</div>',
                                  f'<div class="role-title"><p>{text}</p></div>')

        # Arrows
        if '<div class="arrow' in line and '→' in line:
            line = line.replace('>→<', '><p>→</p><')
        if '<div class="arrow' in line and '←' in line:
            line = line.replace('>←<', '><p>←</p><')
        if '<div class="arrow' in line and '↓' in line:
            line = line.replace('>↓<', '><p>↓</p><')

        # Success circle
        if '<div class="success-circle">' in line:
            # Next lines until </div>
            in_circle = True
            result.append(line)
            continue

        if 'success-circle' in ''.join(result[-3:]) and in_circle and '</div>' in line:
            # Wrap content between
            idx = len(result) - 1
            while idx >= 0 and '<div class="success-circle">' not in result[idx]:
                idx -= 1
            if idx >= 0:
                # Found start, wrap everything between
                circle_content = []
                for i in range(idx + 1, len(result)):
                    circle_content.append(result[i].strip())
                result = result[:idx+1]
                text = '<br/>'.join([c for c in circle_content if c])
                result.append(f'    <p>{text}</p>')
                result.append(line)
                in_circle = False
                continue

        result.append(line)

    return '\n'.join(result)

# Simple fix: just wrap everything obvious
def simple_wrap(filepath):
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()

    # Fix date
    content = re.sub(r'<div class="date">📅([^<]+)</div>',
                    r'<div class="date"><p>📅\1</p></div>', content)

    # Fix role-title
    content = re.sub(r'<div class="role-title">([^<]+)</div>',
                    r'<div class="role-title"><p>\1</p></div>', content)

    # Fix arrows
    content = content.replace('<div class="arrow arrow-left">\n    →\n  </div>',
                             '<div class="arrow arrow-left"><p>→</p></div>')
    content = content.replace('<div class="arrow arrow-right">\n    ←\n  </div>',
                             '<div class="arrow arrow-right"><p>←</p></div>')
    content = content.replace('<div class="arrow arrow-top">\n    ↓\n  </div>',
                             '<div class="arrow arrow-top"><p>↓</p></div>')

    # Fix success circle
    content = re.sub(r'<div class="success-circle">\s*期中\s*<br/>\s*成功\s*</div>',
                    '<div class="success-circle"><p>期中<br/>成功</p></div>', content, flags=re.DOTALL)

    with open(filepath, 'w', encoding='utf-8') as f:
        f.write(content)
    print(f"Fixed: {filepath}")

# Process slide7
simple_wrap('slides/slide7_midterm.html')
simple_wrap('slides/slide8_commitment.html')
