import requests
import os
import html

# --- Création des dossiers ---
os.makedirs("pages", exist_ok=True)

# --- Liste des sourates à générer (sans sourate 55) ---
sourates = [
    {"sourate": 1, "nom": "Al-Fatihah", "nom_ar": "الفاتحة", "versets": [(1, 7)]},
    {"sourate": 2, "nom": "Al-Baqara", "nom_ar": "البقرة", "versets": [(1, 5), (255, 257), (284, 286)]},
    {"sourate": 3, "nom": "Al-Imran", "nom_ar": " آل عمران", "versets": [(1, 9), (18, 19), (26, 27), (190, 200)]},
    {"sourate": 9, "nom": "At-Tawba", "nom_ar": "التوبة", "versets": [(128, 129)]},
    {"sourate": 18, "nom": "Al-Kahf", "nom_ar": "الكهف", "versets": [(107, 110)]},
    {"sourate": 32, "nom": "As-Sajda", "nom_ar": "السجدة", "versets": [(1, 30)]},
    {"sourate": 36, "nom": "Ya-Sin", "nom_ar": "يس", "versets": [(1, 83)]},
    {"sourate": 40, "nom": "Ghafir", "nom_ar": "غافر", "versets": [(1, 3)]},
    {"sourate": 44, "nom": "Ad-Dukhan", "nom_ar": "الدخان", "versets": [(1, 40)]},
    {"sourate": 56, "nom": "Al-Waqiah", "nom_ar": "الواقعة", "versets": [(1, 96)]},
    {"sourate": 57, "nom": "Al-Hadid", "nom_ar": "الحديد", "versets": [(1, 29)]},
    {"sourate": 59, "nom": "Al-Hashr", "nom_ar": "الحشر", "versets": [(1, 24)]},
    {"sourate": 61, "nom": "As-Saff", "nom_ar": "الصف", "versets": [(1, 11)]},
    {"sourate": 62, "nom": "Al-Joumoua", "nom_ar": "الصف", "versets": [(1, 11)]},
    {"sourate": 64, "nom": "At-Taghaboun", "nom_ar": " التغابن", "versets": [(1, 18)]},
    {"sourate": 67, "nom": "Al-Mulk", "nom_ar": "الملك", "versets": [(1, 30)]},
    {"sourate": 87, "nom": "Al-Ala", "nom_ar": " الأعلى", "versets": [(1, 19)]},
    {"sourate": 93, "nom": "Ad-Dhoha", "nom_ar": " الضحى", "versets": [(1, 11)]},
    {"sourate": 94, "nom": "Ash-Sharh", "nom_ar": "الشرح", "versets": [(1, 8)]},
    {"sourate": 96, "nom": "Al-Alaq", "nom_ar": "العلق", "versets": [(1, 19)]},
    {"sourate": 97, "nom": "Al-Qadr", "nom_ar": "القدر", "versets": [(1, 5)]},
    {"sourate": 99, "nom": "Al-Zalzala", "nom_ar": " الزلزلة", "versets": [(1, 8)]},
    {"sourate": 102, "nom": "At-Takathour", "nom_ar": " التكاثر", "versets": [(1, 8)]},
    {"sourate": 103, "nom": "Al-Asr", "nom_ar": "العصر", "versets": [(1, 3)]},
    {"sourate": 106, "nom": "Quraish", "nom_ar": "قريش", "versets": [(1, 4)]},
    {"sourate": 107, "nom": "Al-Maoun", "nom_ar": "الماعون", "versets": [(1, 7)]},
    {"sourate": 108, "nom": "Al-Kaouthar", "nom_ar": "الكوثر", "versets": [(1, 3)]},
    {"sourate": 109, "nom": "Al-Kafiroun", "nom_ar": "الكافرون", "versets": [(1, 6)]},
    {"sourate": 110, "nom": "An-Nasr", "nom_ar": "النصر", "versets": [(1, 3)]},
    {"sourate": 112, "nom": "Al-Ikhlas", "nom_ar": "الإخلاص", "versets": [(1, 5)]},
    {"sourate": 113, "nom": "Al-Falaq", "nom_ar": "الفلق", "versets": [(1, 5)]},
    {"sourate": 114, "nom": "An-Nass", "nom_ar": "الناس", "versets": [(1, 6)]},
]


# --- CSS index (cartes) ---
css_index = """
body {
    background: linear-gradient(to bottom, #f8fafc, #e2e8f0);
    font-family: 'Open Sans', sans-serif;
    text-align: center;
    margin: 0;
    padding: 2em;
}
h1 { color: #1e293b; margin-bottom: 0.5em; }
.container { display: flex; flex-wrap: wrap; justify-content: center; gap: 1em; }
.card {
    background: white;
    border-radius: 16px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
    padding: 1.5em;
    max-width: 200px;
    width: 100%;
    font-size: 1.2em;
    line-height: 1.5em;
    transition: transform 0.2s ease;
    text-decoration: none;
    color: #111;
}
.card:hover { transform: scale(1.05); }
.ar { font-size: 1.5em; display: block; margin-bottom: 0.3em; color: #004d40; }
"""

# --- CSS sourate (lecture simple) ---
css_sourate = """
body {
    font-family: 'Scheherazade New', 'Amiri', serif;
    background-color: #f8f8f8;
    margin: 0;
    padding: 0;
    color: #222;
    line-height: 2;
}
.container {
    max-width: 900px;
    margin: 50px auto;
    background: white;
    padding: 40px;
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
h1.titre-arabe {
    font-size: 2.5em;
    text-align: center;
    color: #004d40;
    margin-bottom: 0.2em;
    font-family: 'Amiri', serif;
}
h2.titre-francais {
    text-align: center;
    color: #555;
    margin-bottom: 1.5em;
    font-weight: normal;
}
.verset {
    font-size: 1.6em;
    text-align: right;
    margin: 0.5em 0;
}
.numero {
    color: #999;
    font-size: 0.8em;
    margin-left: 8px;
}
"""

# --- Générer les pages des sourates ---
for s in sourates:
    print(f"→ Création de la page : {s['nom']} ({s['sourate']})")
    sourate_num = s["sourate"]
    nom = s["nom"]
    nom_ar = s["nom_ar"]
    html_output = f"""
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<title>{nom} - Sourate {sourate_num}</title>
<style>{css_sourate}</style>
</head>
<body>
<div class="container">
<h1 class="titre-arabe">{nom_ar}</h1>
<h2 class="titre-francais">Sourate {nom} ({sourate_num})</h2>
"""

    # Récupérer les versets depuis l'API
    url = f"https://api.quran.com/api/v4/quran/verses/uthmani?chapter_number={sourate_num}"
    resp = requests.get(url, verify=False)
    data = resp.json()
    for start, end in s["versets"]:
        for v in data["verses"]:
            num = int(v["verse_key"].split(":")[1])
            if start <= num <= end:
                texte_ar = html.escape(v["text_uthmani"])
                html_output += f'<p class="verset"><span class="numero">{num}</span> {texte_ar}</p>\n'

    html_output += "</div></body></html>"

    with open(f"pages/{sourate_num:03d}_{nom}.html", "w", encoding="utf-8") as f:
        f.write(html_output)

# --- Générer l'index.html ---
html_index = f"""
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Sourates Méritoires</title>
<style>{css_index}</style>
</head>
<body>
<h1>Sourates Méritoires</h1>
<div class="container">
"""

for s in sourates:
    html_index += f'<a href="pages/{s["sourate"]:03d}_{s["nom"]}.html" class="card">'
    html_index += f'<span class="ar">{s["nom_ar"]}</span>{s["nom"]}</a><br>\n'

html_index += "</div></body></html>"

with open("index.html", "w", encoding="utf-8") as f:
    f.write(html_index)

print("✅ Génération terminée !")
