import re
import random
import glob

# Collect all gallery photos to use as placeholders
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('\\', '/')

friaries_data = {
    "ARCHDIOCESE OF RANCHI": [
        "Franciscan Ashram (Provincial Residence), P.O. Harmu Housing Colony, Ranchi – 834002, JHARKHAND, Estd. 1978",
        "Franciscan Training Institute, Purulia Road (Dr. Camil Bulcke Path), P. Box No. 123, Ranchi – 834001, JHARKHAND, Estd. 1954",
        "Franciscan Ashram Banhora, P.O. Hehel, Ranchi – 834005, JHARKHAND, Estd. 1988",
        "Franciscan Ashram, P.O. Getalsud (Via – Tatisilwai), Ranchi – 835101, JHARKHAND, Estd. 2002"
    ],
    "DIOCESE OF KHUNTI": [
        "St. Anthony’s Monastery, P.O. Dorma (Via – Torpa), Khunti – 835227, JHARKHAND, Estd. 1970",
        "Franciscan Ashram Gaurbera, P.O. Bhamini (Via - Muruhu), Khunti – 835216, JHARKHAND, Estd. 1986",
        "Assisi Bhavan, P.O. Dorma (Via – Torpa), Khunti – 835227, JHARKHAND, Estd. 2006",
        "Vinay Bhavan, P.O. Bichna, Khunti - 835210, Jharkhand, Estd. 2009"
    ],
    "DIOCESE OF SIMDEGA": [
        "Franciscan Ashram Bhukumunda, P.O. Targa, Simdega – 835226, JHARKHAND, Estd. 1987",
        "St. Joseph’s Church Kuruskela, Simdega - 835228, JHARKHAND, Estd. 2012",
        "Catholic Church Behrinbasa, P.O. Behrinbasa, Simdega – 835226, JHARKHAND, Estd. 2013"
    ],
    "DIOCESE OF GUMLA": [
        "Franciscan Ashram Murumkela, P.O. Kansir (Via – Chainpur), Gumla – 835206, JHARKHAND, Estd. 1998"
    ],
    "DIOCESE OF PURNEA": [
        "Franciscan Ashram Thakurganj, P.O. Thakurganj, Kishanganj – 855116, BIHAR, Estd. 1996"
    ],
    "DIOCESE OF BAGDOGRA": [
        "Franciscan Ashram Adhikari, P.O. Adhikari (Via – Khoribari), Darjeeling – 734427, WEST BENGAL, Estd. 1982."
    ],
    "DIOCESE OF JALPAIGURI": [
        "Franciscan Ashram Hasimara, P.O. Hasimara, Jalpaiguri – 735215, WEST BENGAL, Estd. 1984",
        "Franciscan Ashram Chel-Line, Sylee, P.O. Mal, Jalpaiguri - 735221, WEST BENGAL, Estd. 2018"
    ],
    "DIOCESE OF ROURKELA": [
        "Franciscan Ashram (St. Francis Xavier Church) Deorapara, P.O. Lohondabud, Sundergarh – 700022, ODISHA, Estd. 2003",
        "Sneh Bhavan Jamunadipa, P.O. Kuarmunda, Rourkela, ODISHA-770039, Estd. 2014"
    ],
    "DIOCESE OF BONGAIGAON": [
        "Franciscan Ashram Kashiabari, Village Sindrijhora, P.O. Kashiabari Kokrajhar, ASSAM - 783360"
    ],
    "OUTSIDE INDIA": [
        "Pater Nicholas Tudu, TOR, Pfarrei Mariä Himmelfahrt, Schulstr.1, 84051 Essenbach",
        "Pater Daison Thaikkattil, TOR, Kirch Strasse 3a, 79793 Wutoeschingen, Germany"
    ]
}

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 FRIARIES</h2>
"""

for diocese, friaries in friaries_data.items():
    html += f'        <h3 style="font-family: \'Phudu\', sans-serif; font-size: 1.5rem; font-weight: 700; color: #4a2a18; margin-top: 3rem; margin-bottom: 1.5rem; border-bottom: 2px solid #e5e5e5; padding-bottom: 0.5rem;">{diocese}</h3>\n'
    html += '        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 2rem; margin-bottom: 2rem;">\n'
    for f_text in friaries:
        # separate name from address
        parts = f_text.split(',', 1)
        name = parts[0].strip()
        address = parts[1].strip() if len(parts) > 1 else ""
        
        photo = get_photo()
        html += f"""            <div style="border-radius: 12px; overflow: hidden; box-shadow: 0 10px 20px rgba(0,0,0,0.05); display: flex; flex-direction: column; background: #fff;">
                <img src="{photo}" alt="{name}" style="width: 100%; height: 200px; object-fit: cover;">
                <div style="padding: 1.5rem;">
                    <h4 style="font-family: 'Phudu', sans-serif; font-size: 1.1rem; font-weight: 700; color: #1c1917; margin-top: 0; margin-bottom: 0.5rem;">{name}</h4>
                    <p style="font-family: 'Instrument Sans', sans-serif; font-size: 0.95rem; color: #57534e; line-height: 1.5; margin: 0;">{address}</p>
                </div>
            </div>\n"""
    html += '        </div>\n'
html += "    </div>\n"

with open('community-friaries.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-friaries.html', 'w', encoding='utf-8') as f:
        f.write(new_text)
    print("Friaries Success")
else:
    # fallback for different max-width
    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-friaries.html', 'w', encoding='utf-8') as f:
            f.write(new_text)
        print("Friaries Success (Fallback)")
    else:
        print("Match not found for Friaries")
