import re

# Paths
mod_path = r'c:\Users\Utilisateur\Mon Drive\mathmaurer2020\maths_2_2026\00_configuration\modele_qcm.html'
qcm_path = r'c:\Users\Utilisateur\Mon Drive\mathmaurer2020\maths_2_2026\02_ensembles_de_nombres\qcm\qcm_02_ensemble_nombres.html'

with open(mod_path, encoding='utf-8') as f:
    mod = f.read()

with open(qcm_path, encoding='utf-8') as f:
    qcm = f.read()

# 1. Extract QCM Data
qcm_data_match = re.search(r'const allQcmData = \{[\s\S]*?\n\};', qcm)
qcm_data_str = qcm_data_match.group(0) if qcm_data_match else ""
qcm_data_str = qcm_data_str.replace('"explanation":', '"correction":')

# 2. Extract Navigation Menu
nav_match = re.search(r'<ul class="flex flex-col md:flex-row flex-wrap items-center space-y-2 md:space-y-0 md:space-x-4 lg:space-x-6 list-none mt-2 md:mt-0 text-white">.*?</ul>', qcm, re.DOTALL)
nav_str = nav_match.group(0) if nav_match else ""

# 3. Apply to Model
new_html = mod

# Inject QCM Data
new_html = re.sub(r'const allQcmData = \{[\s\S]*?\n\};', lambda m: qcm_data_str, new_html)

# Inject Navigation Menu
new_html = re.sub(r'<ul class="flex flex-col md:flex-row flex-wrap items-center space-y-2 md:space-y-0 md:space-x-4 lg:space-x-6 list-none mt-2 md:mt-0 text-white">.*?</ul>', lambda m: nav_str, new_html, flags=re.DOTALL)

# Fix SEO Metadata (was wrongly copied from chapter 01)
new_seo = '''					fr: {
						title: "QCM Ensembles de Nombres - Maths 2nde | MathMaurer",
						description: "QCM interactif pour tester ses connaissances sur les définitions et propriétés des ensembles de nombres en classe de seconde.",
						keywords: "qcm, ensembles de nombres, maths seconde, mathmaurer",
						url: "https://www.mathmaurer.com/maths_2_2026/02_ensembles_de_nombres/qcm/qcm_02_ensemble_nombres.html"
					}'''
new_html = re.sub(r'fr: \{\s*title: "QCM [^"]+",\s*description: "[^"]+",\s*keywords: "[^"]+",\s*url: "[^"]+"\s*\}', new_seo, new_html)

# Fix Titles
new_html = new_html.replace('TITRE DU CHAPITRE', 'Ensembles de Nombres')
new_html = new_html.replace('CHAPTER TITLE', 'Sets of Numbers')
new_html = new_html.replace('TÍTULO DEL CAPÍTULO', 'Conjuntos de Números')

# Save updated QCM
with open(qcm_path, 'w', encoding='utf-8') as f:
    f.write(new_html)

print("QCM 02 successfully updated with the model!")
