import re
import random
import glob

gallery_photos = glob.glob('assets/images/gallery/*.jpg') + glob.glob('assets/images/gallery/*.JPG') + glob.glob('assets/images/gallery/*.jpeg')
if not gallery_photos:
    gallery_photos = ['https://images.unsplash.com/photo-1548625361-ec853c528f80?w=800&q=80']

def get_photo():
    return random.choice(gallery_photos).replace('\\', '/')

friars_text = """Fr. Anselm Kullu
Fr. George Palamattam
Fr. Jerome Alookaran
Br. Simon Gahatraj
Fr. Cyril Kochuvilayil
Fr. Marianus Kujur
Fr. Hilarius Barla
Fr. John Thakadiyel
Fr. Nicholas Tudu
Fr. Donatus Xaxa
Fr. Anthony Hemrom
Fr. Donatus Tirkey
Fr. Anthony Soren
Fr. Alphonse Ekka
Fr. Philip Kaduthanam
Fr. Mathew Kanayinkal
Br. Oscar Hemrom
Fr. George Mailadil
Br. Subhash P. Tete
Fr. Herman Kujur
Fr. John Horo
Fr. Vijay Kumar Ekka
Fr. Bigit Bage
Fr. Ajit Xalxo
Fr. Daison Thaikattil
Fr. Anand Kumar Barla
Fr. Leander Kerketta
Fr. Esidor Baa
Fr. Suman Kishore Dhan
Fr. Isidore Kerketta
Fr. Carolus Kiro
Fr. Ranjan Prakash Ekka
Fr. Abhay Tigga
Fr. Manoj Vengathanam
Fr. Manoj Kullu
Fr. Shaji Alapurath
Fr. Benjamin Tiru
Fr. Vincy Illickal
Fr. Xavier Kindo
Fr. Raju Tirkey
Fr. Anthers Bara
Fr. Ambose Lakra
Fr. Benedict Tirkey
Fr. Paulinus Kiro
Fr. Bijay Prakash Tirkey
Fr. Joseph Toppo
Fr. Gigesh Meckel
Fr. Ajit Tiru 
Fr. Anup Toppo
Fr. Deepak Ekka
Fr. Amit Ekka
Fr. Chander Kujur
Fr. Sumit Bilsan Tigga
Fr. Ajeet Paul Kerketta
Fr. Naveen Kujur
Fr. Pradeep Lakra
Fr. Prashant Xalxo
Fr. Wilson Lugun
Fr. Anshu Anand Tiru
Fr. Matias Kandulna
Fr. Anand Barla
Fr. Mukesh Ekka
Fr. Rohit Ekka
Fr. Anil Soreng
Fr. Amit Kumar Beck
Fr. Flourence Tirkey
Fr. Jugal Barla
Fr. Udit lakra
Fr. Albert Kindo
Fr. Sujit Lakra
Fr. Sumit Tirkey
Fr. Kuldeep Ekka
Br. Ajit Topno
Br. Kishor Jaria
Fr. Santosh Toppo
Br. Mukesh Toppo
Fr. Jasman Toppo
Br. Anselm Mundu
Br. Oskar Soreng
Br. Amardeep Lakra
Br. Harshit Kerketta
Br. William Minz
Br. Roshan Soreng
Br. Bikash Kiro"""

necrology_text = """Br. Carlus Bara 		28.07.2002 
Br. Michael Bhengra 		23.07.2003 
Deacon Sabu Vettikottil 	13.12.2003 
Fr. Dionis Kiro 		29.02.2008 
Fr. James Lakra 		03.06.2013 
Fr. Augustine Peedikamala 	14.10.2014 
Br. Benedict Kullu 		28.01.2021 
Fr. Gregory Kerketta 		20.04.2021 
Fr. Donatus Soreng 		11.05.2021 
Fr. Fabian Kunnappillil 	20.01.2025 
Fr. Sushil Prawin Tiru 	07.01.2026"""

html = """    <div style="max-width: 1200px; margin: 0 auto; padding: clamp(3rem, 8vw, 5rem) 2rem;">
        <h2 style="font-family: 'Phudu', sans-serif; font-size: 2.2rem; font-weight: 900; color: #1c1917; margin-bottom: 2.5rem; text-align: center;">OUR FRIARS</h2>
        
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 2rem; margin-bottom: 5rem;">
"""

for line in friars_text.strip().split('\n'):
    name = line.strip()
    if not name: continue
    
    # Let's try to match specifically for Manoj if applicable
    photo = get_photo()
    if "Manoj Vengathanam" in name:
        photo = "assets/images/gallery/Fr. Manoj Vengathanam Minister Provincial.jpg"
        
    html += f"""            <div style="text-align: center;">
                <div style="width: 140px; height: 140px; border-radius: 50%; overflow: hidden; margin: 0 auto 1rem auto; box-shadow: 0 10px 20px rgba(0,0,0,0.1);">
                    <img src="{photo}" alt="{name}" style="width: 100%; height: 100%; object-fit: cover;">
                </div>
                <h4 style="font-family: 'Phudu', sans-serif; font-size: 1rem; font-weight: 700; color: #4a2a18; margin: 0;">{name}</h4>
            </div>\n"""

html += """        </div>
        
        <h2 style="font-family: 'Phudu', sans-serif; font-size: 2.2rem; font-weight: 900; color: #1c1917; margin-bottom: 2.5rem; text-align: center; border-top: 1px solid #e5e5e5; padding-top: 4rem;">NECROLOGY</h2>
        <div style="max-width: 600px; margin: 0 auto;">
            <ul style="list-style: none; padding: 0; margin: 0;">
"""

for line in necrology_text.strip().split('\n'):
    parts = line.strip().split()
    if len(parts) >= 3:
        date = parts[-1]
        name = " ".join(parts[:-1])
        html += f"""                <li style="display: flex; justify-content: space-between; align-items: center; padding: 1rem 0; border-bottom: 1px dashed #e5e5e5;">
                    <span style="font-family: 'Phudu', sans-serif; font-size: 1.1rem; font-weight: 700; color: #1c1917;">{name}</span>
                    <span style="font-family: 'Instrument Sans', sans-serif; font-size: 1rem; color: #666;">{date}</span>
                </li>\n"""

html += """            </ul>
        </div>
    </div>\n"""

with open('community-friars.html', 'r', encoding='utf-8') as f:
    text = f.read()

pattern = r'<div style="max-width: 950px; margin: 0 auto; padding: clamp\(3rem, 8vw, 5rem\) 2rem;">.*?</div>\s*<!-- FOOTER \+ BOTTOM WIDGETS'
match = re.search(pattern, text, re.DOTALL)
if match:
    new_text = text[:match.start()] + html + '    <!-- FOOTER + BOTTOM WIDGETS' + text[match.end():]
    with open('community-friars.html', 'w', encoding='utf-8') as f:
        f.write(new_text)
    print("Friars Success")
else:
    pattern2 = r'<div style="max-width: \d+px; margin: 0 auto; padding: clamp\(3rem, 8vw, 5rem\) 2rem;">.*?</div>\s*<!-- FOOTER \+ BOTTOM WIDGETS'
    match2 = re.search(pattern2, text, re.DOTALL)
    if match2:
        new_text = text[:match2.start()] + html + '    <!-- FOOTER + BOTTOM WIDGETS' + text[match2.end():]
        with open('community-friars.html', 'w', encoding='utf-8') as f:
            f.write(new_text)
        print("Friars Success (Fallback)")
    else:
        print("Match not found for Friars")
